Create n amount of arrays
I would like to know how, if possible, to create a n amount of arrays of the same size. Help would be greatly appreci开发者_运维技巧ated. 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.
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
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
Two dimensional array
http://www.leepoint.net/notes-java/data/arrays/arrays-2D.html
int[] i = {1,2,3,4,5}
int[] j = i.clone()
You will get the size alone with the contents
精彩评论