-1

I'm trying to convert and Arrayylist of type string to a 2d-array, String[][] using the following way

    ArrayList<String> arrayList = new ArrayList<String>(); 
    arrayList.add("report1");
    arrayList.add("report2");
    arrayList.add("report3");

    String[][] array = new String[arrayList.size()][];
    for (int i = 0; i < arrayList.size(); i++) {
        ArrayList<String> row = arrayList.get(i);
        array[i] = row.toArray(new String[row.size()]);
    }

resulting a compilation error at ArrayList<String> row = arrayList.get(i);

Need suggestions to resolve this or is there anyother way I can achieve the same

5
  • 2
    You can only get a one-dimensional arrayList. You have an ArrayList<String>, not ArrayList<ArrayList<String>>. Commented Jul 24, 2013 at 11:49
  • Are you trying to convert a single dimensional list into two dimensional String array? Commented Jul 24, 2013 at 11:51
  • yes, I'm trying to convert it to 2d String array necessarily. Commented Jul 24, 2013 at 11:54
  • maybe this can help you stackoverflow.com/questions/2146488/… Commented Jul 24, 2013 at 11:59
  • 1
    @Mango: without knowing your desired output it is impossible to answer. Commented Jul 24, 2013 at 12:00

3 Answers 3

0

You only have an ArrayList, not ArrayList>. The former is a dynamic array of strings and you can get String[] out of it.

Simply call

String[] myArray=arrayList.toArray(new String[0]);

No need to get the size down as it'll get the size automatically.

If you wanted to get a char[][] then do:

char[][] chars=new char[arrayList.size()][0]
int i=0;
for(String s:arrayList){
    chars[i]=s.toCharArray();
    i++;
}

Note that we're incrementing a counter in the loop. The enhanced for loop doesn't provide us a counter.

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

4 Comments

that is for 2D arrays?
@MaVRoSCy No, the OP doesn't even have a 2D array at hand. They only have a single arraylist of strings.
converting it to String[] wouldnot help me
@Mango I can't help you as Java is neither magic nor psychic. You don't have a 2-D arrayList so it can't be converted to a 2D array. f you did have a 2D array I'd need to know how you want to split rows.
0
    // TODO Auto-generated method stub
    ArrayList<String> arrayList = new ArrayList<String>(); 
    arrayList.add("report1");
    arrayList.add("report2");
    arrayList.add("report3");
    arrayList.add("report4");
    arrayList.add("report5");
    arrayList.add("report6");
    arrayList.add("report7");
    arrayList.add("report8");
    arrayList.add("report9");

    int rowSize = 3;



    String[][] array = new String[rowSize][];
    int rowIndex=0;

    ArrayList<String> tempList = new ArrayList<String>();
    for (int i = 0; i < arrayList.size(); i++) {

        tempList.add(arrayList.get(i));

        if(i>0 && ((i+1)%(rowSize))==0){

            array[rowIndex++] = tempList.toArray(new String[tempList.size()]);
            tempList = new ArrayList<String>();
        }
    }

    // prints the result array for varification

    for(int i=0;i<array.length;i++){
        System.out.println("\n\nRow : "+i+"\n");
        String[] innerArray = array[i];
        for(int j=0;j<innerArray.length;j++){
            System.out.print(innerArray[j]+" ");
        }
    }

Change the rowSise variable value as you desired.

Comments

0

arrayList.get(i) it will return String not a arraylist of string. so compiler will give casting error.

if you want to achieve your functionality you can do it as following

  String[][] array = new String[arrayList.size()][];
  for (int i = 0; i < arrayList.size(); i++) {
      ArrayList<String> row = new ArrayList<String>();
      row.add(arrayList.get(i));
      array[i] = row.toArray(new String[row.size()]);
  }

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.