英文:
How to print the all the elements of String type arrays which present inside an list?
问题
public class 列表{
public static void main(String[] args) {
// TODO Auto-generated method stub
// 动态数组列表,其中输入是动态的,就像下面的示例一样
ArrayList<String[]> 行 = new ArrayList<>();
行.add(new String[]{"1", "2", "3"});
行.add(new String[]{"1", "2"});
行.add(new String[]{"1"});
for (String[] 当前行 : 行) {
System.out.println(java.util.Arrays.toString(当前行));
}
}
}
我在控制台收到了这个输出,有人可以帮忙解决如何打印动态变化长度的数组。
英文:
public class list{
public static void main(String[] args) {
// TODO Auto-generated method stub
//Dynamic arraylist in which is input is dynamic like below is example
ArrayList<String[]> rows = new ArrayList<>();
rows.add(new String[]{"1","2","3"});
rows.add(new String[]{"1","2"});
rows.add(new String[]{"1"});
for (String[] now : rows) {
System.out.println(now);
}
}
}
I am receiving this output in console can anyone help how to print the dynamically changing length of array.
[Ljava.lang.String;@372f7a8d
[Ljava.lang.String;@2f92e0f4
[Ljava.lang.String;@28a418fc
答案1
得分: 0
你必须在第一个循环内部再添加一个循环,因为你有一个数组列表中嵌套着数组。
英文:
You have to add another loop inside the first loop since you have an array list of array
答案2
得分: 0
因为您试图打印一个字符串数组引用而不是字符串,所以您得到了这样的输出。请尝试以下代码。
public class list {
public static void main(String[] args) {
ArrayList<String[]> rows = new ArrayList<>();
rows.add(new String[]{"1","2","3"});
rows.add(new String[]{"1","2"});
rows.add(new String[]{"1"});
for (String[] now : rows) {
for(int i = 0 ; i < now.length ; i++){//Printing the current array elements
System.out.println(now[i]);
}
}
}
}
英文:
You are getting an output like this because you are trying to print an String array reference, instead of string.Try the below code.
public class list{
public static void main(String[] args) {
ArrayList<String[]> rows = new ArrayList<>();
rows.add(new String[]{"1","2","3"});
rows.add(new String[]{"1","2"});
rows.add(new String[]{"1"});
for (String[] now : rows) {
for(int i = 0 ; i < now.length ; i++){//Printing the current array elements
System.out.println(now[i]);
}
}
}
}
答案3
得分: 0
如果您想打印出数组的所有元素:
解决方案1:
for (String[] now : rows) {
System.out.println(Arrays.toString(now));
}
解决方案2:
for (String[] now : rows) {
for (String i : now) {
System.out.println(i);
}
}
英文:
If you want to print all the elements of the arrays:
Solution 1:
for (String[] now : rows) {
System.out.println(Arrays.toString(now));
}
Solution 2:
for (String[] now : rows) {
for (String i : now) {
System.out.println(i);
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论