Converting each line from a list of integers into an array of integers?

淺唱寂寞╮ 提交于 2020-12-15 06:16:34

问题


I'm new to Java, so apologies if any of this is unclear - I want to convert a list of four-digit integers into a list of integer arrays - i.e if I have an integer that is 4567, I want to convert that into an array of four separate integers [4, 5, 6, 7]. So I want to convert each line/index of the list into it's own array (rather than converting the entire list into an array). I'm currently reading a file that has four-digit integers (each on a new line) and adding them to a list, which is then returning that list - any ideas on how could I code it so that it returns a list of integer arrays instead?

    public List <Integer> loadGuesses (String fileName){

    List<Integer> loadGuessList = new ArrayList<>();

    try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
        String line;
        while ((line = reader.readLine()) != null) {
            loadGuessList.add(Integer.parseInt(line));
        }
    } catch (FileNotFoundException f){
        System.out.println("File not found - please re-enter with correct file name.");
        getPlayerFile();
    } catch (IOException e){
        System.out.println("Incorrect file name - Please re-enter with correct file name.");
        getPlayerFile();
    }
    return loadGuessList;

}

回答1:


Change your type of loadGuessList to List<int[]>.

After reading the string, you can iterate through the characters and convert each to an int.

  List<int[]> loadGuessList = new ArrayList<>();

  while ((line = reader.readLine()) != null) {
    int [] digits = new int[4];
    for (int i = 0; i < line.length(); i++) { //Assuming line.length() will be 4
        digits[i] = line.charAt(i) - '0';
    }
    loadGuessList.add(digits);
}

Reference: How can I convert a char to int in Java?




回答2:


To convert a string with a number into a array of integers with the digits of the text, use this method:

public static int[] toDigits(String text) {
    int[] digits = new int[text.length()];
    for (int i = 0; i < text.length(); i++)
        digits[i] = Character.digit(text.charAt(i), 10);
    return digits;
}

Test

System.out.println(Arrays.toString(toDigits("4567")));
System.out.println(Arrays.toString(toDigits("0014789632500")));
System.out.println(Arrays.toString(toDigits("123FOO")));

Output

[4, 5, 6, 7]
[0, 0, 1, 4, 7, 8, 9, 6, 3, 2, 5, 0, 0]
[1, 2, 3, -1, -1, -1]



回答3:


you can try this:

public static void main(String[] args) throws Exception {
    List<Integer> list = new ArrayList<Integer>();

    BufferedReader reader = new BufferedReader(new FileReader("data.txt"));
    String line = null;
    while((line = reader.readLine()) != null ) {
        char[] tmp = line.toCharArray();
        for(int i=0; i<tmp.length; i++) {
            list.add(Character. getNumericValue(tmp[i]));
        }

    }
    // TODO release resources
}



回答4:


List class already provides a convenient method ( toArray ) for converting itself to an array. You can use the below snippet as a reference for your problem.


    public Integer[] loadGuesses(String fileName) {
            List loadGuessList = new ArrayList();

            /**
             * all of your code.
             */

            return loadGuessList.toArray(new Integer[loadGuessList.size()]);
    }




回答5:


You can just use the toCharArray method of the String class:

loadGuessList.add(line.toCharArray())

Now loadGuessList is a List<Chat[]>.




回答6:


I highly recommend the the Guava which is produced by Google to handle the string or collections if you need it. maybe it is not suit for this topic,but you will get benifits from it in your later projects.



来源:https://stackoverflow.com/questions/52143146/converting-each-line-from-a-list-of-integers-into-an-array-of-integers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!