How to combine elements from the list taking into account index of the list?

EdXX

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?

muasif80

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.

edited at
0

Comments

0 comments
Login to comment

Related

Taking Elements From a List

How to recursively combine pairs of elements from a list?

How to combine elements from a data list in R?

How to combine specific elements from a Python list?

How to Sort a list taking in account multiple factors?

How to delete elements from a List synchronously by Index?

How to combine like elements in list?

Changing the xpath by taking elements from a list

Taking variable number of elements from list

How to combine elements in list to a new nested list?

How to combine elements from a List without duplicates using Java

How to combine elements from a list with both strings and integers

How do I combine elements from a list in R?

Taking the rest of the list into account in filter

How do I match exact strings from a list to a larger string taking white spaces into account?

jQuery: how to sort list text taking into account numerical values

use Streams to generate a new list taking into account all the first list elements

How to combine elements in set and a string to a list in python?

How to combine elements of list? (C#)

How to combine all sublist elements into one list

How to combine elements in a complicated list in R?

How to combine all the elements of list to a dataframe in R

How to combine a list of services from different array elements into one list in flutter

Combine list elements by suffix

Combine elements of a list

Combine elements of a list in R

How to print every third element from current index in list of elements

How to compare the index of 2 sublists' elements from a list of lists

How to make a function to remove elements by index from a list