0

So in my program I split the first row of data imported by a csv file into an array. Is there anyway that I can add this array into an array list as the first element? Because once I split the second data into an array by a delimiter I then want to store this array in the same arraylist but in element 2. A bit confusing but to summarize is nested arrays in an arraylist possible?

public static ArrayList<String[]> readCSV(Scanner csv, String delimiter, int minCellsPerRow) {
  String line = csv.nextLine();
  String[] parts = line.split(delimiter);
  List<String> list = new ArrayList<String>();
  list.add(parts);
}
2
  • list.add(list.size(), parts) Commented Mar 18, 2014 at 1:19
  • tag this post as Java, and syntax highlighting turns on :). Commented Mar 18, 2014 at 1:38

1 Answer 1

1

you can specify insertion index with list.add()... here is an example:

public static void main(String[] args) {

    //setup
    ArrayList<String> storage;

    storage = new ArrayList<String>(Arrays.asList("4","5","6"));

    String[] data = {"1","2","3"};

    printMe(storage);

    //append
    storage.addAll(0, Arrays.asList(data));

    printMe(storage);
}

public static void printMe(ArrayList<String> strs) {
    System.out.println(Arrays.toString(strs.toArray(new String[0])));
}

yields the console result:

[4, 5, 6]
[1, 2, 3, 4, 5, 6]

would this work in your case?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.