提问人:SCoffers Rel 提问时间:7/4/2023 最后编辑:SCoffers Rel 更新时间:7/6/2023 访问量:66
多维数组不断重复
Multidimensional Array keeps repeating
问:
我是 java 的新手,不幸的是,我正在为此苦苦挣扎。我似乎无法让数组在每次迭代中停止重复。我在输出中组合线条时也遇到了问题。我尝试使用扫描仪,但我似乎也无法让它工作。
这是我尝试过的:
`// Online Java Compiler
使用此编辑器在线编写、编译和运行 Java 代码
import java.util.Scanner;
class ArrayStudies {
public static void main(String[] args) {
//* Define and initialize the ID Array *//
int[] id = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
//* Define the UserID Array *//
String[] userid = new String[10];
//* Initialize the Names Array *//
userid[0] = "Admin";
userid[1] = "Vale.Vicky";
userid[2] = "Lane.Lois";
userid[3] = "Kent.Clark";
userid[4] = "Wayne.Bruce";
userid[5] = "Parker.Peter";
userid[6] = "Rogers.Steve";
userid[7] = "Luther.Lex";
userid[8] = "Osborn.Harry";
userid[9] = "Prince.Diana";
//* Define the Password Array *//
String[] password = new String[10];
//* Initialize the Password Array *//
password[0] = "Password1";
password[1] = "ZZZZZZZ";
password[2] = "VVVVVV";
password[3] = "AAAAAA";
password[4] = "FFFFFFFF";
password[5] = "RRRRRRR";
password[6] = "QQQQQQ";
password[7] = "GGGGGG";
password[8] = "YYYYYY";
password[9] = "LLLLLLL";
//*Use data to Create Multidemensional Array*//
for (int l = 0; l < id.length; l++) {
for (int i = 0; i < userid.length; i++)
System.out.println("ID: " + id[i] + " " + "UserID: " + userid[i] + " " + "Password:" + " " + password[i]);
}
}
}
答:
0赞
Reilas
7/5/2023
#1
使用一个 for 循环。
//*Use data to Create Multidemensional Array*//
String[][] strings = new String[10][2];
for (int index = 0; index < 10; index++) {
strings[index][0] = userid[index];
strings[index][1] = password[index];
}
输出
strings[0] = [Admin, Password1]
strings[1] = [Vale.Vicky, ZZZZZZZ]
strings[2] = [Lane.Lois, VVVVVV]
strings[3] = [Kent.Clark, AAAAAA]
strings[4] = [Wayne.Bruce, FFFFFFFF]
strings[5] = [Parker.Peter, RRRRRRR]
strings[6] = [Rogers.Steve, QQQQQQ]
strings[7] = [Luther.Lex, GGGGGG]
strings[8] = [Osborn.Harry, YYYYYY]
strings[9] = [Prince.Diana, LLLLLLL]
评论
for (int l
Foo [][] foo2D;
Foo [][][] foo 3D;
Foo [][][][] foo4D;
class Person { int id; String name; String password; Person (int id, String name, String pass) { this.id = id; this.name = name; this.password = pass; } // getters & setters, if any ... }
Person [] = { new Person (0, "Admin","Password1"), new Person (1, "Vale.Vicky", "ZZZZZZZ"), ... }
record Person(int id, String name, String pass) { }