如何打印列表中所有包含在字符串类型数组中的元素?

huangapple 未分类评论43阅读模式
英文:

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&lt;String[]&gt; rows = new ArrayList&lt;&gt;();
	      rows.add(new String[]{&quot;1&quot;,&quot;2&quot;,&quot;3&quot;});
	      rows.add(new String[]{&quot;1&quot;,&quot;2&quot;});
	      rows.add(new String[]{&quot;1&quot;});
		
            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&lt;String[]&gt; rows = new ArrayList&lt;&gt;();
          rows.add(new String[]{&quot;1&quot;,&quot;2&quot;,&quot;3&quot;});
          rows.add(new String[]{&quot;1&quot;,&quot;2&quot;});
          rows.add(new String[]{&quot;1&quot;});
        
          for (String[] now : rows) {
            for(int i = 0 ; i &lt; 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);
      }
}

huangapple
  • 本文由 发表于 2020年7月26日 23:54:15
  • 转载请务必保留本文链接:https://java.coder-hub.com/63102431.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定