I have an array of lists
List<String>[] tab = new ArrayList[5]
Let say my tab lists look like:
tab[0]={1242}
tab[1]={London, Paris}
tab[2]={England, France}
tab[3]={Finance}
tab[4]={No}
Now I need to make lines from above like
1242 London England Finance No
1242 Paris France Finance No
That means that if I have more than 1 element in particular lists then I need to take those elements using indexes - first with first, second with second and join them with the others. In addition I don't know in advance in which tab[?] I will get multiple elements. Can anyone help me and show how to do this?
Ok. You can iterate through the array starting from index 1. Then inside the first loop have a second loop. There you concatenate the strings from the lists and if list has only one element then just concatenate it.
List<String>[] tab = new ArrayList[5];
tab[0] = new ArrayList<String>(Arrays.asList("1242"));
tab[1] = new ArrayList<String>(Arrays.asList("London", "Paris"));
tab[2] = new ArrayList<String>(Arrays.asList("England", "France"));
tab[3] = new ArrayList<String>(Arrays.asList("Finance"));
tab[4] = new ArrayList<String>(Arrays.asList("No"));
StringBuffer buffer;
for(int j=1; j<=2; j++){
buffer = new StringBuffer(tab[0].get(0)).append(" ");
for(int i = 1; i < tab.length; i++){
List<String> list = tab[i];
if(list.size() == 1){
buffer.append(list.get(0)).append(" ");
}else{
buffer.append(list.get(j - 1)).append(" ");
}
}
System.out.println(buffer.toString());
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments