views:

135

answers:

9

Hello All,

I have a following ArrayList,

[Title,Data1,Data2,Data3]
[A,2,3,4]
[B,3,5,7]

And I would like to convert this one like this,

[Title,A,B]
[Data1,2,3]
[Data2,3,5]
[Data3,4,7]

I'm bit confused with the approach. Any hint would be much appreciated.

Thanks.

+1  A: 

You need to use two dimensional array. Look here.

Itay
+1  A: 

Check whether all lists have the same size.

Place the informations in a Matrix (List with List elements, for example) to get the number of lists and size of a list.

Create a new Matrix with the rotated size informations. (3x4 to 4x3)

Implement 2 For loops and place the elements into the new matrix.

Markus Lausberg
+2  A: 

This called a transpose operation. A code sample is here, , but will need significant modification as you have an ArrayList of Arrays (what I infer from your question)

Midhat
+1  A: 

Do you have a fixed number of ArrayLists and are they of fixed size to begin with? If it is fixed, what you can do is have an int index value and process each ArrayList in turn in the same loop. You can then transfer each value to a temporary ArrayList and then place a reference to this in a final ArrayList for output.

Sounds confusing? Here's a rough solution:

ArrayList tempList = new ArrayList();
ArrayList outputList = new ArrayList();

for(index=0;index<list1.getsize();index++){
// Add null checks and other validation here
tempList.add( list1.get(index) );
tempList.add( list2.get(index) );
tempList.add( list3.get(index) );
outputList.add( tempList );
}
James P.
+3  A: 

something like this maybe

List<List<String>> list = new ArrayList<List<String>>(firstList.size());
for(int i = 0; i < firstList.size(); i++) {
   list.add(Arrays.asList(
      firstList.get(i),
      secondList.get(i),
      thirdList.get(i))
   );
}
al nik
+2  A: 

The mathematics behind: you need to transpose the matrix. It's easier if you use a 2-dimensional array or a 'List of Lists', which is pretty much he same with collections. A List of arrays works too, but it is slightly more confusing.

This wikipedia article shows some algorithms for transposition.

Andreas_D
Interesting. Are you considering the three Lists as a matrix? If so, it is transposition as you say.
James P.
Sure, what else? Looks like List<Object[]> structure to me.
Andreas_D
I have eyes but I cannot see. More seriously this is still fresh from an exam and I need to get into the reflex of spotting things like this out. A matrix-like structure would indeed be more general purpose.
James P.
+1  A: 

If it is for a datamigration task, you might consider your friendly spreadsheet if the size is not too big.

For matrix manipulation stuff there is the jScience library which has matrix support. For just transposing a metrix this would be overkill, but it depends what needs to be done with it.

Peter Tillemans
+4  A: 

This is called transposition. The following snippet does what you need:

import java.util.*;
public class ListTranspose {
    public static void main(String[] args) {
        Object[][] data = {
            { "Title", "Data1", "Data2", "Data3" },
            { "A", 2, 3, 4 },
            { "B", 3, 5, 7 },
        };
        List<List<Object>> table = new ArrayList<List<Object>>();
        for (Object[] row : data) {
            table.add(Arrays.asList(row));
        }
        System.out.println(table); //  [[Title, Data1, Data2, Data3],
                                   //   [A, 2, 3, 4],
                                   //   [B, 3, 5, 7]]"
        table = transpose(table);
        System.out.println(table); //  [[Title, A, B],
                                   //   [Data1, 2, 3],
                                   //   [Data2, 3, 5],
                                   //   [Data3, 4, 7]]
    }
    static <T> List<List<T>> transpose(List<List<T>> table) {
        List<List<T>> ret = new ArrayList<List<T>>();
        final int N = table.get(0).size();
        for (int i = 0; i < N; i++) {
            List<T> col = new ArrayList<T>();
            for (List<T> row : table) {
                col.add(row.get(i));
            }
            ret.add(col);
        }
        return ret;
    }
}

See also

polygenelubricants
+1 Perfect and Thank you @polygenelubricants. This is simply works. Trying to find the logic behind this :)
TuxGeek
I'm a bit rusty on generics. What does the <T> notation mean? Is it a sort of placeholder for an Object?
James P.
@James: it's a type parameter. http://java.sun.com/j2se/1.5.0/docs/guide/language/generics.html
polygenelubricants
+2  A: 

The technique is called transposing. Example of an implementation.

public static MyObject [][] transpose(MyObject [][] m){
    int r = m.length;
    int c = m[r].length;
    MyObject [][] t = new MyObject[c][r];
    for(int i = 0; i < r; ++i){
        for(int j = 0; j < c; ++j){
            t[j][i] = m[i][j];
        }
    }
    return t;
}
Fergal