0

I have 3 arraylist each have size = 3 and 3 arrays also have length = 3 of each. I want to copy data from arraylists to arrays in following way but using any loop (i.e for OR for each).

enter image description here

myArray1[1] = arraylist1.get(1);
myArray1[2] = arraylist2.get(1);
myArray1[3] = arraylist3.get(1);

I have done it manually one by one without using any loop, but code appears to be massive because in future I'm sure that number of my arraylists and arrays will increase up to 15.

I want to copy the data from arraylists to arrays as shown in the image but using the loops not manually one by one?

halfer
  • 19,824
  • 17
  • 99
  • 186
Qadir Hussain
  • 8,721
  • 13
  • 89
  • 124

3 Answers3

1

You need to have two additional structures:

int[][] destination = new int [][] {myArray1, myArray2,myArray3 }
List<Integer>[] source;
    source = new List<Integer>[] {arraylist1,arraylist2,arraylist3}

    myArray1[1] = arraylist1.get(1);
    myArray1[2] = arraylist2.get(1);
    myArray1[3] = arraylist3.get(1);

    for (int i=0;i<destination.length;i++) {
      for (int j=0;j<source.length;j++) {
        destination[i][j] = source[j].get(i);
      }
    }
Igor S.
  • 3,332
  • 1
  • 25
  • 33
1

How about this?

    List<Integer> arraylist0 = Arrays.asList(2,4,3);
    List<Integer> arraylist1 = Arrays.asList(2,5,7);
    List<Integer> arraylist2 = Arrays.asList(6,3,7);
    List<List<Integer>> arraylistList = Arrays.asList(arraylist0, arraylist1, arraylist2);

    int size = 3;
    int[] myArray0 = new int[size];
    int[] myArray1 = new int[size];
    int[] myArray2 = new int[size];
    int[][] myBigArray = new int[][] {myArray0, myArray1, myArray2};

    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            myBigArray[i][j] = arraylistList.get(j).get(i);
        }
    }

To explain, since we want to be able to work with an arbitrary size (3, 15, or more), we are dealing with 2-dimensional data.

We are also dealing with array and List, which are slightly different in their use.

The input to your problem is List<Integer>, and so we make a List<List<Integer>> in order to deal with all the input data easily.

Similarly, the output will be arrays, so we make a 2-dimensional array (int[][]) in order to write the data easily.

Then it's simply a matter of iterating over the data in 2 nested for loops. Notice that this line reverses the order of i and j in order to splice the data the way you intend.

    myBigArray[i][j] = arraylistList.get(j).get(i);

And then you can print your answer like this:

    System.out.println(Arrays.toString(myArray0));
    System.out.println(Arrays.toString(myArray1));
    System.out.println(Arrays.toString(myArray2));
vikingsteve
  • 38,481
  • 23
  • 112
  • 156
-1

If you cannot find a ready made API or function for this, I would suggest trivializing the conversion from List to Array using the List.toArray() method and focus on converting/transforming the given set of lists to a another bunch of lists which contain the desired output. Following is a code sample which I would think achieves this. It does assume the input lists are NOT of fixed/same sizes. Assuming this would only make the logic easier.

On return of this function, all you need to do is to iterate over the TreeMap and convert the values to arrays using List.toArray().

public static TreeMap<Integer, List<Integer>> transorm(
        List<Integer>... lists) {

    // Return a blank TreeMap if not input. TreeMap explanation below.
    if (lists == null || lists.length == 0)
        return new TreeMap<>();

    // Get Iterators for the input lists
    List<Iterator<Integer>> iterators = new ArrayList<>();
    for (List<Integer> list : lists) {
        iterators.add(list.iterator());
    }

    // Initialize Return. We return a TreeMap, where the key indicates which
    // position's integer values are present in the list which is the value
    // of this key. Converting the lists to arrays is trivial using the
    // List.toArray() method.
    TreeMap<Integer, List<Integer>> transformedLists = new TreeMap<>();

    // Variable maintaining the position for which values are being
    // collected. See below.
    int currPosition = 0;

    // Variable which keeps track of the index of the iterator currently
    // driving the iteration and the driving iterator.
    int driverItrIndex = 0;
    Iterator<Integer> driverItr = lists[driverItrIndex].iterator();

    // Actual code that does the transformation.
    while (driverItrIndex < iterators.size()) {

        // Move to next driving iterator
        if (!driverItr.hasNext()) {
            driverItrIndex++;
            driverItr = iterators.get(driverItrIndex);
            continue;
        }

        // Construct Transformed List
        ArrayList<Integer> transformedList = new ArrayList<>();
        for (Iterator<Integer> iterator : iterators) {
            if (iterator.hasNext()) {
                transformedList.add(iterator.next());
            }
        }

        // Add to return
        transformedLists.put(currPosition, transformedList);
    }

    // Return Value
    return transformedLists;
}
rgeorge
  • 303
  • 1
  • 8
  • I guess too much help, hence beyond requirements, hence downvoted? Still learning SO. – rgeorge May 22 '13 at 13:09
  • 1
    You could also use Matrix classes in a math library and call transpose on it. Apache Common Maths has a Matrices package. – rgeorge May 23 '13 at 03:10