我想做类似这样的事情 ArrayList & lt; String< String< String> & gt; my list
我怎样才能创造它?
如何加入外部和内部名单?
如何将内部列表转换为简单的数组列表?
我想做类似这样的事情 ArrayList & lt; String< String< String> & gt; my list
我怎样才能创造它?
如何加入外部和内部名单?
如何将内部列表转换为简单的数组列表?
你可以一起走
List<List<String>> ls2d = new ArrayList<List<String>>();
List<String> x = new ArrayList<String>();
x.add("Hello");
x.add("world!");
ls2d.add(x);
System.out.println(Arrays.deepToString(ls2d.toArray()));
第一个阵列列表不是字符串的阵列列表,而是矩阵列表的阵列列表。
ArrayList<ArrayList<String>>
List<List<String>> array = new ArrayList<List<String>>();
...
array.add(new ArrayList<String>())
array.get(0).add("qqq");
数组.get(0) - 是一个内部列表
List<List<String>> super2dArray = new ArrayList<ArrayList<String>>()
这是持有字符串的矩阵列表的矩阵列表 。
还有ArrayList< String< String< String>> my list
没有任何意义,因为字符串不是一个收藏/列表,但我认为你理解这一点。未来读者可能不会这样做。
见“https://stackoverflow.com/a/8896608/645270><这个答案 ,以了解为什么我选择左侧有List
。
实现你的愿望有两种方式。我为两者提供代码片断:
1. 2. List<List< String> & gt; lol = 新的ArrayList< List< String> & gt; (;)
Scanner in = new Scanner(System.in);
int size = in.nextInt();
//Declare your two dimensional ArrayList of Strings.
List< List<String>> lol = new ArrayList<List<String>>();
//Instantiate and Populate
for (int i=0;i<size;i++){
int internalListSize = in.nextInt(); //the size of each internal list
lol.add(new ArrayList<String>());
for (int j=0;j<internalListSize;j++){
String whateverYouWanttoPut = in.nextLine();
lol.get(i).add(whateverYouWanttoPut);
}
}
//Access Elements
try {
System.out.println(lol.get(0).get(4));
System.out.println(lol.get(1).get(2));
System.out.println(lol.get(3).get(2));
} catch (Exception e){
System.out.println("ERROR!");
}
ArrayList[] 设置 =新的ArrayList[n];
Scanner in = new Scanner(System.in);
int size = in.nextInt();
//Declare your two dimensional ArrayList of Strings.
ArrayList[] set = new ArrayList[size];
//Populate it.
for(int i=0;i<size;i++){
int innerSize = in.nextInt();
set[i] = new ArrayList<String>();
for(int j=0;j<innerSize;j++){
set[i].add(in.nextLine());
}
}
try{
System.out.println(set[0].get(1));
System.out.println(set[1].get(2));
} catch(Exception e){
System.out.println("ERROR!");
}
尝试一下这个 :
public class JavaTests {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String[ ][ ] test2 = new String[3][3]; //3 can be replace if you add more test2[?][?] = "RandomString"
test2[0][0] = "String1";
test2[0][1] = "String2";
test2[0][2] = "String3";
test2[1][0] = "String4";
test2[1][1] = "String5";
test2[1][2] = "String6";
test2[2][0] = "String7";
test2[2][1] = "String8";
test2[2][2] = "String9";
for (int i = 0; i <= 2; i++){
for (int j = 0; j <= 2; j++){
System.out.print(test2[i][j] +" ");
}
System.out.println("
");
}
System.out.println("
");
}
}
创建 2D 阵列, 如下面 -
原始数据类型的 < 强 > - 强>
int[][] arr = new int[10][10];
用于自定义类的 < em% 强 > - 强/ 强/ 强 >
public class MyClass {
int x;
MyClass() {
this.x = 0
}
}
MyClass[][] arr = new MyClass[10][10];
for (int i = 0; i < 10; ++i) {
for (int j = 0; j < 10; ++j) {
arr[i][j] = new MyClass();
}
}
Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...
Check this, List<String> list = new ArrayList<String>(); for (int i = 0; i < 10000; i++) { String value = (""+UUID.randomUUID().getLeastSignificantBits()).substring(3, ...
I am in the middle of solving a problem where I think it s best suited for a decorator and a state pattern. The high level setting is something like a sandwich maker and dispenser, where I have a set ...
I have been trying to execute a MS SQL Server stored procedure via JDBC today and have been unsuccessful thus far. The stored procedure has 1 input and 1 output parameter. With every combination I ...
I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...
If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...
I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....
I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...