Inicjalizacja tablicy wielowymiarowej w Javie
Jaki jest prawidłowy sposób deklarowania tablicy wielowymiarowej i przypisywania jej wartości?
Oto co mam:
int x = 5;
int y = 5;
String[][] myStringArray = new String [x][y];
myStringArray[0][x] = "a string";
myStringArray[0][y] = "another string";
7 answers
Spróbuj zamienić odpowiednie wiersze na:
myStringArray[0][x-1] = "a string";
myStringArray[0][y-1] = "another string";
Twój kod jest nieprawidłowy, ponieważ tablice podrzędne mają długość y
, A indeksowanie zaczyna się od 0. Ustawienie na myStringArray[0][y]
lub myStringArray[0][x]
nie powiedzie się, ponieważ indeksy x
i y
są poza granicami.
String[][] myStringArray = new String [x][y];
jest prawidłowym sposobem inicjalizacji prostokątnej tablicy wielowymiarowej. Jeśli chcesz, aby była postrzępiona (każda tablica może mieć inną długość), możesz użyć kodu podobnego do tej odpowiedzi . Uwaga jednak twierdzenie Johna, że trzeba ręcznie tworzyć podzbiory, jest nieprawidłowe W przypadku, gdy chcemy mieć idealnie prostokątną tablicę wielowymiarową.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 10:31:37
Java nie ma "prawdziwych" tablic wielowymiarowych.
Na przykład, arr[i][j][k]
jest równoważne ((arr[i])[j])[k]
. Innymi słowy, arr
jest po prostu tablicą, tablicami, tablicami.
Więc jeśli wiesz, jak działają tablice, wiesz, jak działają tablice wielowymiarowe!
Deklaracja:
int[][][] threeDimArr = new int[4][5][6];
Lub, z inicjalizacją:
int[][][] threeDimArr = { { { 1, 2 }, { 3, 4 } }, { { 5, 6 }, { 7, 8 } } };
Dostęp:
int x = threeDimArr[1][0][1];
Lub
int[][] row = threeDimArr[1];
String reprezentacja:
Arrays.deepToString(threeDimArr);
"[[[1, 2], [3, 4]], [[5, 6], [7, 8]]]"
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-07-25 21:56:58
Możesz również użyć następującej konstrukcji:
String[][] myStringArray = new String [][] { { "X0", "Y0"},
{ "X1", "Y1"},
{ "X2", "Y2"},
{ "X3", "Y3"},
{ "X4", "Y4"} };
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-04-14 22:22:30
Możesz zadeklarować tablice wielowymiarowe takie jak:
// 4 x 5 String arrays, all Strings are null
// [0] -> [null,null,null,null,null]
// [1] -> [null,null,null,null,null]
// [2] -> [null,null,null,null,null]
// [3] -> [null,null,null,null,null]
String[][] sa1 = new String[4][5];
for(int i = 0; i < sa1.length; i++) { // sa1.length == 4
for (int j = 0; j < sa1[i].length; j++) { //sa1[i].length == 5
sa1[i][j] = "new String value";
}
}
// 5 x 0 All String arrays are null
// [null]
// [null]
// [null]
// [null]
// [null]
String[][] sa2 = new String[5][];
for(int i = 0; i < sa2.length; i++) {
String[] anon = new String[ /* your number here */];
// or String[] anon = new String[]{"I'm", "a", "new", "array"};
sa2[i] = anon;
}
// [0] -> ["I'm","in","the", "0th", "array"]
// [1] -> ["I'm", "in", "another"]
String[][] sa3 = new String[][]{ {"I'm","in","the", "0th", "array"},{"I'm", "in", "another"}};
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-08-19 08:30:30
Wielowymiarowa tablica w Javie
Zwracanie tablicy wielowymiarowej
Java Nie truely Obsługa tablic wielowymiarowych. W języku Java tablica dwuwymiarowa jest po prostu tablicą tablic, tablica trójwymiarowa jest tablicą tablic tablic, tablica czterowymiarowa jest tablicą tablic tablic tablic i tak dalej...
Możemy zdefiniować tablicę dwuwymiarową as:
int[ ] num[ ] = {{1,2}, {1,2}, {1,2}, {1,2}}
-
int[ ][ ] num = new int[4][2]
num[0][0] = 1; num[0][1] = 2; num[1][0] = 1; num[1][1] = 2; num[2][0] = 1; num[2][1] = 2; num[3][0] = 1; num[3][1] = 2;
Jeśli nie przydzielisz, powiedzmy
num[2][1]
, nie zostanie zainicjowany i zostanie automatycznie przydzielone 0, czyli automatycznienum[2][1] = 0
; Poniżej
num1.length
podano wiersze.- podczas gdy
num1[0].length
daje liczbę elementów związanych znum1[0]
. Tutajnum1[0]
ma tylko powiązane tablicenum1[0][0]
inum[0][1]
. -
Tutaj użyliśmy pętli
for
, która pomaga nam obliczyćnum1[i].length
. Tutaji
jest inkrementowane przez pętlę.class array { static int[][] add(int[][] num1,int[][] num2) { int[][] temp = new int[num1.length][num1[0].length]; for(int i = 0; i<temp.length; i++) { for(int j = 0; j<temp[i].length; j++) { temp[i][j] = num1[i][j]+num2[i][j]; } } return temp; } public static void main(String args[]) { /* We can define a two-dimensional array as 1. int[] num[] = {{1,2},{1,2},{1,2},{1,2}} 2. int[][] num = new int[4][2] num[0][0] = 1; num[0][1] = 2; num[1][0] = 1; num[1][1] = 2; num[2][0] = 1; num[2][1] = 2; num[3][0] = 1; num[3][1] = 2; If you don't allocate let's say num[2][1] is not initialized, and then it is automatically allocated 0, that is, automatically num[2][1] = 0; 3. Below num1.length gives you rows 4. While num1[0].length gives you number of elements related to num1[0]. Here num1[0] has related arrays num1[0][0] and num[0][1] only. 5. Here we used a 'for' loop which helps us to calculate num1[i].length, and here i is incremented through a loop. */ int num1[][] = {{1,2},{1,2},{1,2},{1,2}}; int num2[][] = {{1,2},{1,2},{1,2},{1,2}}; int num3[][] = add(num1,num2); for(int i = 0; i<num1.length; i++) { for(int j = 0; j<num1[j].length; j++) System.out.println("num3[" + i + "][" + j + "]=" + num3[i][j]); } } }
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-12-04 20:42:12
Dodam, że jeśli chcesz przeczytać wymiary, możesz to zrobić:
int[][][] a = new int[4][3][2];
System.out.println(a.length); // 4
System.out.println(a[0].length); // 3
System.out.println(a[0][0].length); //2
Można również mieć postrzępione tablice , gdzie różne wiersze mają różną długość, więc a[0].length != a[1].length
.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-10-27 09:04:38
Możesz spojrzeć na to, aby zacząć:
int [][][] i = { //third dimension curly brace
{ // second dimension curly brace
{ //first dimension curly brace
1,1,1 //elements
},
{3,3,3},
{2,2,2}
},
{
{
1,1,1
},
{3,3,3},
{2,2,2}
}
};
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-08-15 11:43:01