1

I would like to know how, if possible, to create a n amount of arrays of the same size. Help would be greatly appreciated. For example: I want to create 10 arrays with the same amount of elements without having to create them one by one: int[] a = new int[]. Hope this is more clear now.

One of my questions in one of the comments was +- "how do I sort the array row for row / column for column". I figured it out - maybe someone may find it useful.

int[] sortarr = new int[5]; //create array to transfer data from row to new array

for (int i=0; i<N; i++){
    for (int j=0; j<5; j++){
    sortarr[j] = hands[i][j]; //transfer the data from 2D array's row to sortarr
    }
    Arrays.sort(sortarr); //sort the row's data

    for (int x=0; x<5; x++){ 
    hands[i][x] = sortarr[x]; //transfer the data back to 2D array
    }

}

Maybe it's pretty obvious, but I hope this will help someone out there.

ISJ
  • 509
  • 2
  • 8
  • 17

4 Answers4

5

You need to create a 2D array.

int n;
int size;
int[][] arr = new int[size][n];

You can fill the array with a nested for loop;

for(int i =0;i < arr.length; i++) {
    for(int j = 0; i < arr[i].length; j++) {
        arr[i][j] = someValue;
    }
}

Or you could populate the arrays like so:

int[][] arr2 = new int[n][];
for(int i = 0; i < n; i++){
    arr2[i] = new int[size];
}

You can think of a 2D array as an array of arrays, for example:

private Card[][] allPlayerHands;
public Card[] getHand(int playerNumber) {
    return allPlayerHands[playerNumber];
}

Here is a good Stack Overflow question about 2D arrays:

Initialising a multidimensional array in Java

Community
  • 1
  • 1
jjnguy
  • 136,852
  • 53
  • 295
  • 323
  • I'm not sure that an 2D array will work. I want to shuffle a deck and now deal the cards to 5 different people. That five cards need to be saved each in its own array. – ISJ Feb 23 '11 at 21:34
  • @ISJ, you can think of a 2D array as an array of arrays. See my edit. – jjnguy Feb 23 '11 at 21:35
  • @ISJ made another edit in the middle of the post for creating many 1D arrays in a 2D array. – jjnguy Feb 23 '11 at 21:44
  • @jjnguy, thanks a lot. So I can just save player one's cards in the first row. Player two's cards in row in the second row, etc. Thus: int[][] a = new int[players][5] - 5 card poker – ISJ Feb 23 '11 at 21:45
  • @jjnguy - sorry for bothering, but I have another question concerning a 2D array. How do I sort the array row for row? Thanks, ISJ – ISJ Feb 24 '11 at 06:16
  • @ISJ, check out, `Arrays.sort()`. You can pass in one of the arrays that is part of the 2D array and it will only sort that part. – jjnguy Feb 24 '11 at 07:58
  • @ISJ, but if you want to sort each array in the 2D array, that's a different issue. – jjnguy Feb 24 '11 at 07:58
  • @jjnguy Thanks for all the help. I figured it out. I pasted the code in my question – ISJ Feb 24 '11 at 11:54
3

2D array is the answer. Let me try to explain

you need to deal the deck to 5 different people i.e. int people[5].

now consider, that each 5 people have 5 cards i.e

Guy 1: 1,2,3,4,5
Guy 2: 1,2,3,4,5
Guy 3: 1,2,3,4,5
Guy 4: 1,2,3,4,5
Guy 5: 1,2,3,4,5

i.e.

people[1]: 1,2,3,4,5
people[2]: 1,2,3,4,5
people[3]: 1,2,3,4,5
people[4]: 1,2,3,4,5
people[5]: 1,2,3,4,5

i.e.

people[5][5]

now if you have to access, card 3 of person 1 then it would be

people[0][2] // u know its zero based aray
x.509
  • 2,205
  • 10
  • 44
  • 58
0

Two dimensional array

http://www.leepoint.net/notes-java/data/arrays/arrays-2D.html

Evgeni Dimitrov
  • 21,976
  • 33
  • 120
  • 145
0

int[] i = {1,2,3,4,5}

int[] j = i.clone()

You will get the size alone with the contents