如何访问数组的元素,该数组是ArrayList的一个元素?

时间:2017-11-24 18:52:39

标签: java arrays arraylist text-files

我在java方面不专业,但我尽我所能。我的代码读取一个文本文件,然后放入一个数组,然后将其每个索引放入一个数组列表,但我的问题是我必须访问该数组的元素,因为我的数组看起来像这样(名字,姓氏,测验1,测验2, midterm,project,final,average)其实我的ArrayLists第一个元素是这样的,例如,我试图访问中期结果,然后将其打印到表中。

static List<Assign2> studentList = new ArrayList<>();
public static void main(String [] args) throws IOException{
        new myClass();


        File here = new File(".");
        System.out.println(here.getAbsolutePath());
        BufferedReader reader = new BufferedReader(new FileReader("A.txt"));
        String line= reader.readLine();
        String[] arr =line.split(",");


        Object[] data = {arr[0],arr[1],arr[2],arr[3]};
        //studentList.add(Arrays.asList(data));



    }
public static void table(List<myClass> b){
            System.out.printf("%-20s %-20s %-20s %-20s %-20s %-20s %-20s %-20s %-20s %-20s", "NAME","LAST NAME","ID","QUIZ1","QUIZ2","PROJECT","MIDTERM","FINAL","AVERAGE","LETTER GRADE");
            System.out.println();
            System.out.println("-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
            for(int i=0;i<b.size();i++){
                System.out.printf("%-20s %-20s %-20s %-20s %-20s %-20s %-20s %-20s %-20s %-20s",b.get(i).getName(),b.get(i).getLastName(),b.get(i).getID(),b.get(i).getQuiz1(),b.get(i).getQuiz2(),b.get(i).getProject(),b.get(i).getMidterm(),b.get(i).getFinalGrade(),b.get(i).getAverage(),b.get(i).getLetterGrade());
                System.out.println();
            }
        }

2 个答案:

答案 0 :(得分:1)

你应该这样做:

List<myClass> studentList = new ArrayList<>();

myClass obj=new myClass();

studentList.Add(obj);

...

for(int i=0;i<studentList.Count;i++)
{
  myClass studentObject=studentList[index];
  // do staff
}

答案 1 :(得分:1)

创建一个保存变量(名称,姓氏等)的类,然后创建该类类型的ArrayList(ArrayList)并调用所需的项目并访问所需的数据是不是更容易