4

How to convert 2d string array into ArrayList?? I have a 2d array of string, how can i covert it to array list???

1
  • depends on how you want to concat them. Commented Oct 22, 2012 at 14:16

3 Answers 3

3

If you want a full list of elements just do this. Probably there is a simple way

   ArrayList<String> list = new ArrayList<String>();
   for (int i=0; i < array_limit ; i++)
       for (int j=0 ; j < array_limit; j++)
              list.add(your_array[i][j]);
Sign up to request clarification or add additional context in comments.

2 Comments

then how can i relate array itmes?? like how can I find array[2][1]??
@user1756754 if your array is xy then array[2][1]=list.get((y*2)+1)........ that is for any i and j array[i][j]=list.get((yi)+j)
1
public static ArrayList<String> rowsToString(String[][] data) {
    ArrayList<String> list = new ArrayList<String>();

    for(int i = 0; i < data.length; i++) {
        String row = Arrays.toString(data[i]);
        list.add( row.substring(1, row.length()-1) );
    }

    return list;
}

Comments

1

Depends on what exactly you want. Try:

for(int i=0;i<a.length;i++){

        newList.add(new ArrayList<String>());
        for(int j=0;j<a.length;j++){
        newList.get(i).add(a[i][j]);
        }

    }

Then you can access elements for example by:

newList.get(1).get(2);

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.