1_bug
1_bug

Reputation: 5717

Getting access to records in list of arrays

I need to create list of n arrays and fill only first record in every array with records from arr array (first record of first array will be equal to first record from arr array, first record of second array will be equal to second record from arr array etc.).

In C# it's simple:

        List<Point[]> grupy = new List<Point[]>();          //create n array of Point
        for (int i = 1; i <= n; i++)
            grupy.Add(new Point[10]);                       //array length will be 10

        int a = 0;
        foreach (Point[] x in grupy)                         
        {                                                   
            grupy[a][0] = new Point(arr[a]);                //fill first records in all arrays form list
            a++;
        }

But I need this code in Java and I noticed that I can't call to records in array from list like that:

    List<Point[]> grupy = new ArrayList<Point[]>();     //create n arrays of Point
        for (int i = 1; i <= n; i++)
            grupy.add(new Point[10]);

    int a = 0;
    for(Point[] x : grupy)                              
    {                                                   
        grupy[a][0] = new Point(arr[a]);         //WRONG WAY
        a++;
    }

grupy.get(a)[0] dosen't working too. So, my question is: how can I get access to specified record in array from list of arrays?

I know that I can create temp array in for(), save arr.get(a) to temp[0] and add temp to grupy but after i need to display or change specified record and this way won't help.

Upvotes: 0

Views: 182

Answers (2)

herau
herau

Reputation: 1516

If you works with java 8, you can use the Streams API with forEach, map and filter functions

Upvotes: 0

jhamon
jhamon

Reputation: 3691

int a = 0;
for(Point[] x : grupy)                              
{                                                   
    x[0] = arr[a];
    a++;
}

Upvotes: 2

Related Questions