简体   繁体   中英

How to store 5 lines of data as 1 element in an ArrayList?

I have a file of data, it contains the 5 lines of data for albums, and the are 500 Albums. The code below stores every line as a new element, however I was to save 5 lines as 1 element, is this possible and how would I change the code below to do this?

String file_name = "Top500Albums.txt";
String line;
ArrayList aList = new ArrayList();

try {
    BufferedReader input = new BufferedReader(new FileReader(file_name));
    if (! input.ready()){
        throw new IOException();
    }
    while ((line = input.readLine()) !=null) {
        aList.add(line);
    }
    input.close();
}
catch(IOException e) {
    System.out.println(e);
}

int sz = aList.size();
for (int i=0; i< sz; i++)
{
    System.out.println(aList.get(i).toString());
}

You can convert your arraylist data into string array like this

Your ArrayList

List<String> list = new ArrayList<String>();

and you can convert to String array using toArray() method

String[] stringArray = list.toArray(new String[0]);

If you want to 'convert' an ArrayList to an array you can use the following piece of code:

private String[] toArray(ArrayList<String> arrayList) {
    String[] array = new String[arrayList.size()];
    arrayList.toArray(array);
    return array;
} 

But you should ask yourself why you need to work with an array. An ArrayList is simply a view of an internal array, so lookups are not slow like with other types of Lists and using the above method, you will be using twice the memory (you'll be copying the internal array to a new one) until your ArrayList is garbage collected.

How to store 5 lines of data as 1 element in an ArrayList?

You create a Java object that holds 5 lines.

Since you didn't tell us what the content of those 5 lines are, I'm just naming them line1 through line5. You should give these fields more meaningful names.

package com.ggl.testing;

public class Album {

    private final String line1;
    private final String line2;
    private final String line3;
    private final String line4;
    private final String line5;

    public Album(String line1, String line2, String line3, String line4,
            String line5) {
        this.line1 = line1;
        this.line2 = line2;
        this.line3 = line3;
        this.line4 = line4;
        this.line5 = line5;
    }

    public String getLine1() {
        return line1;
    }

    public String getLine2() {
        return line2;
    }

    public String getLine3() {
        return line3;
    }

    public String getLine4() {
        return line4;
    }

    public String getLine5() {
        return line5;
    }

}

Then, you create a List of Album objects.

List<Album> albumList = new ArrayList<>(500);

You add an album to the List like this.

// Read five lines from the file
albumList.add(new Album(line1, line2, line3, line4, line5));

You can maintain a counter to see if every new album data is started

    String file_name = "Top500Albums.txt";
    String line;
    ArrayList aList = new ArrayList();

    int lineCounter = 1;
    try {
        StringBuilder albumData = new StringBuilder();
        BufferedReader input = new BufferedReader(new FileReader(file_name));
        if (!input.ready()) {
            throw new IOException();
        }
        while ((line = input.readLine()) != null) {
            if(lineCounter%5 == 0) {
                albumData.append(line);
                aList.add(albumData.toString());
                albumData.setLength(0);
            } else
                albumData.append(line+"\n");
            lineCounter++;
        }
        input.close();
    } catch (IOException e) {
        System.out.println(e);
    }

    int sz = aList.size();
    for (int i = 0; i < sz; i++) {
        System.out.println(aList.get(i).toString());
    }

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