0
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
beginner
  • 11
  • 4

3 Answers3

0

You have to add another loop inside the first loop since you have an array list of array

0

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]);
            }
        }
}
}
Hasindu Dahanayake
  • 1,344
  • 2
  • 14
  • 37
0

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);
      }
}