简体   繁体   English

如何在静态main方法中将数据添加到arraylist

[英]How can I add data into an arraylist in static main method

I try to read data from a text file and add the data into an arraylist whose element is 7-number array. 我尝试从文本文件中读取数据,并将数据添加到元素为7号数组的arraylist中。 But all the elements of the arraylist always turn to the value which is read last time, which means the last data read replace all the elements in the list. 但是arraylist的所有元素总是变成上次读取的值,这意味着最后读取的数据将替换列表中的所有元素。 I guess the reason might be the function has to be called in the main method which is static. 我想原因可能是必须在静态的main方法中调用该函数。 How can I add the data successfully? 如何成功添加数据?

package main;

import java.util.Locale;
import java.util.Scanner;
import javax.swing.JFileChooser;
import java.util.ArrayList;
import java.io.*;

public class readFile {     

    public static void main(String args[]){
        read();
    }

    public void read()throws Exception{
        Scanner input = new Scanner(new File("./src/sample.txt"));
        input.useLocale(Locale.US);

        double[] temp = new double[7];
        ArrayList<double[]> pointList= new ArrayList<double[]>();

        while(input.hasNext()){
            for (int i=0;i<=6;i++)
            {
                temp[i]=input.nextDouble();
            }
            pointList.add(temp);
        }
    }
}

The data in text file is as the bottom in the figure, while the result is just same for each element. 文本文件中的数据位于图中的底部,而每个元素的结果都相同。

在此处输入图片说明

Move the declaration of temp inside the while loop: 在while循环内移动temp的声明:

    ArrayList<double[]> pointList= new ArrayList<double[]>();

    while(input.hasNext()){
        double[] temp = new double[7];
        for (int i=0;i<7;i++)  // Don't make it more confusing by using "6" here.
        {
            temp[i]=input.nextDouble();
        }
        pointList.add(temp);
    }

Otherwise, you are just repeatedly adding the same array into the list, meaning you are overwriting the elements. 否则,您只是重复将同一数组添加到列表中,这意味着您将覆盖元素。

Adding an item to a list doesn't make a copy of it: it just adds the reference to the item to the list. 将项目添加到列表不会复制该项目:它只是将对该项目的引用添加到列表中。 As such, subsequent changes to that item are reflected in the item in the list. 这样,对该项目的后续更改将反映在列表中的项目中。

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

相关问题 如何在静态方法中将其添加到arraylist? - How do I add this to an arraylist within a static method? 如何在main方法的静态方法下引用变量? - How can I refer to a variable under a static method in the main method? 如何从内部方法向ArrayList添加值? - How can I add a value to an ArrayList from an inner method? 为什么我们不能在main()方法之外向ArrayList添加元素? - why can't we add elements to ArrayList outside of the main() method? 一个 arrayList 方法,它读取文件并从收集的数据中返回 ArrayList。 我如何在我的主要方法或其他方法中调用该数组? - An arrayList method that reads a file and returns an ArrayList from the collected data. how do i call that array in my main method or others? 如何将其添加到数组列表中? - How can I add this to an arraylist? 如何将数据从XML文件添加到ArrayList - How can I add data from an XML file to an ArrayList 如何在主类中创建类x的对象同时在类x中向ArrayList添加值 - How can I add value to ArrayList in class x while the making object of class x in the main class 如何将int数据从静态void方法返回到main方法? - How do I return an int data from a static void method to main method? 如何在方法中添加静态变量号? - How can i add the static variable number in a method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM