Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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);
}
share|improve this question
    
list.add(list.size(), parts) –  Muhammad Tauseef Mar 18 at 1:19
    
tag this post as Java, and syntax highlighting turns on :). –  justinmreina Mar 18 at 1:38

1 Answer 1

up vote 1 down vote accepted

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?

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.