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.

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

share|improve this question
    
depends on how you want to concat them. –  njzk2 Oct 22 '12 at 14:16

3 Answers 3

up vote 2 down vote accepted

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]);
share|improve this answer
1  
then how can i relate array itmes?? like how can I find array[2][1]?? –  Ahmad Abbasi Oct 22 '12 at 14:00
    
@user1756754 a bit of math –  blackbelt Oct 22 '12 at 14:07
    
@user1756754 if your array is x*y then array[2][1]=list.get((y*2)+1)........ that is for any i and j array[i][j]=list.get((y*i)+j) –  maninder singh Oct 22 '12 at 14:15
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;
}
share|improve this answer

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);
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.