简体   繁体   中英

Separate file into ArrayList<ArrayList<Double>>

I have a file in the form:

2.3 2.5
1.4 4.5
....
NaN NaN
2.2 1.4
4.6 5.6
....

(2 columns of doubles which are occasionally both equal to "NaN")

Basically I want to separate both columns into:

ArrayList<Double> x;
ArrayList<Double> y;

While both numbers in the columns are not equal to "NaN" i want to add them into array x and y. When the BufferedReader reads a line:

NaN NaN

i want to add the ArrayList x and y to allX and allY.

ArrayList<ArrayList<Double>> allX;
ArrayList<ArrayList<Double>> allY;

I then want to start a new ArrayList x and y and continue reading data into them until i reach another NaN NaN line (where i would repeat the above process) or the end of the file.

So in the end i am left with 2 ArrayLists of ArrayLists of doubles, the x and y data.

I cant think of a way to do this, any ideas?


If it helps with understanding my problem: the file data is Latitude and Longitude data for every country's border in the world, each country is separated by (lat,lon)=(NaN,NaN). I need to separate the data into an ArrayList for each country which is all contained in a parent ArrayList.

Currently I have:

BufferedReader br = new BufferedReader(new FileReader(new File("file.txt")));
String line;
String[] data;
while((line=br.readLine())!=null){
    data=line.split(" "); //A String array with x and y (could be x="NaN" y="NaN")
    //How do I then process this?
}

Just to give you an idea. I know it's not the best code in the world but it is to give you an enough of info so that you can proceed. Hope it helps. The code is self explanatory, if you find any difficulty please let me know.

public class MyMain1 {
    private static final String CONSTANT = "NaN";

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("/Users/Desktop/xyz.txt"));

        List<List<Double>> allX = new ArrayList<>();
        List<List<Double>> allY = new ArrayList<>();
        List<Double> x = new ArrayList<>();
        List<Double> y = new ArrayList<>();
        try {
            for (String str = null; (str = br.readLine()) != null; ) {
                String[] s = str.split(" ");
                if (CONSTANT.equals(s[0])) {
                    allX.add(x);
                    allY.add(y);
                    x = new ArrayList<>();
                    y = new ArrayList<>();
                } else {
                    x.add(Double.parseDouble(s[0]));
                    y.add(Double.parseDouble(s[1]));
                }
            }
            if (x.size() > 0 && y.size() > 0) {
                allX.add(x);
                allY.add(y);
            }
        } finally {
            br.close();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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