简体   繁体   English

填充 arraylist。 Java

[英]Filling an arraylist. Java

I have a String I split into tokens.我有一个字符串,我拆分为令牌。 I am trying to fill each array list with a for loop.我正在尝试用 for 循环填充每个数组列表。 I'm pretty close but when I test print out the array lists, it is not filling right.我已经很接近了,但是当我测试打印出数组列表时,它没有正确填充。

import java.io.*;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;

public class VicePresidents {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        String filename = "VicePresidentAges.csv";
        File file = new File(filename);
        Scanner infile = new Scanner(file);

        ArrayList<String> names = new ArrayList<String>();
        ArrayList<Integer> ages = new ArrayList<Integer>();
        
        while (infile.hasNext())
        {
            
            String line = infile.nextLine();
            String[] tokens = line.split(",");
            for(int i = 0; i < tokens[0].length(); i++)
            {
                names.add(tokens[0]);
                
            }
            for(int j = 0; j < tokens[1].length(); j++)
            {
                ages.add(Integer.parseInt(tokens[1]));
            }
            System.out.println(names);
            System.out.println(ages);
                        
            //Divide the line into its tokens (There should be 2 tokens per line)
            //put the tokens into their correct ArrayList
        }
        
        infile.close();

        //Go through the ages ArrayList looking for the youngest age.  
        //Hold onto the *index* where the youngest age is located in the ArrayList.
        
        
        
        //Print out the name and age for the youngest Vice President using the index
        //you just found from above
                
            
    }

}

My output looks like this: [John Adams, John Adams, John Adams, John Adams, John Adams, John Adams] with the ages below it like this: [53, 53, 53, 53, 45, 45, 65, 65, 68, 68, 42, 42, 42, 42, 50, 50]我的 output 看起来像这样: [John Adams, John Adams, John Adams, John Adams, John Adams, John Adams]年龄如下: [53, 53, 53, 53, 45, 45, 65, 65, 68, 68, 42, 42, 42, 42, 50, 50]

Made some modifications to your code to get you started, try to understand and maybe improve it (extract methodes, use java.nio to read files, ...)对您的代码进行了一些修改以帮助您入门,尝试理解并改进它(提取方法,使用 java.nio 读取文件,...)

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class VicePresidents {

    public static void main(String args[]) throws IOException {
        String filename = "VicePresidentAges.csv";
        File file = new File(filename);
        Scanner infile = new Scanner(file);

        ArrayList<String> names = new ArrayList<>();
        ArrayList<Integer> ages = new ArrayList<>();

        while (infile.hasNext()){
            String line = infile.nextLine();
            //Divide the line into its tokens (There should be 2 tokens per line)
            String[] tokens = line.split(",");
            //put the tokens into their correct ArrayList
            names.add(tokens[0]);
            ages.add(Integer.parseInt(tokens[1]));
        }        
        infile.close();
        //print both list just to check
        System.out.println(names);
        System.out.println(ages);

        //Go through the ages ArrayList looking for the youngest age.  
        //Hold onto the *index* where the youngest age is located in the ArrayList.
        int youngestIndex = 0;
        for(int i = 0; i < ages.size(); i++){
            if(ages.get(i) < ages.get(youngestIndex)){
                youngestIndex = i;
            }
        }

        //Print out the name and age for the youngest Vice President using the index
        //you just found from above
        System.out.println("Youngest Vice President:");
        System.out.println("Name: " + names.get(youngestIndex) + " age: " + ages.get(youngestIndex));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM