简体   繁体   中英

print Arraylist without loop

I want to print elements in my Arraylist without using a loop of any kind is that possible in java?

import java.util.ArrayList;

class Person
{
    String name;
    String role;

    public Person(String name, String role)
    {
        this.name = name;
        this.role = role;
    }
}

class Main
{
    public static void main(String[] args)
    {
        Person person1 = new Person("george","programmer");
        Person person2 = new Person("barack","programmer");
        Person person3 = new Person("ismail","programmer");

        ArrayList <Person> people = new ArrayList <Person>();
        people.add(person1);
        people.add(person2);
        people.add(person3);

        System.out.println(people.get(0));
        System.out.println(people.get(1));
        System.out.println(people.get(2));
    }
}

No, it is not possible.

Every approach will loop over the values of the array list in certain form. Be it a normal for loop or forEach or the built-in toString() - every approach will loop or iterate over the values of the list in some form.

It depends on how you define "loop". It can be done using a recursive method:

void print( ArrayList<Person> a, int index ) {
   if ( (a != null) && (index < a.size()) ) {
      System.out.println(a.get(index));
      print( a, ++index );
   }
}

then call print( people, 0 );

可能必须获取对象调用tostring方法才能打印数据,否则您将获取对象引用

people.forEach(person -> System.out.println(person));

The above one-liner uses the stream-functionality introduced in Java-8. Conceptually, you're still iterating through the input-elements one by one. But it's more concise than using a for-loop, if that's what you're looking for.

You can print one by one without using loop but the problem is because you are using object type so you cannot print objects unless you use a toString method in the class even if you don't use Arraylist it wont work if you want to print object, unless you use toString method

    class Main
    {
        public static void main(String[] args)
        {  
            ArrayList<Integer> arrayList = new ArrayList<Integer>();
            arrayList.add(6);
            arrayList.add(3);
            arrayList.add(5);

            System.out.println(arrayList.get(0));
            System.out.println(arrayList.get(1));
            System.out.println(arrayList.get(2));
        }
    } 
  • It is very simple way to print array without using any loop in JAVA.

    -> For, Single or simple array:

     int[] array = new int[]{1, 2, 3, 4, 5, 6}; System.out.println(Arrays.toString(array));

    The Output:

     [1, 2, 3, 4, 5, 6]

    -> So, this 2D array can't be printed with Arrays.toString()

     int[][] array = new int[][]{{1, 2, 3, 4, 5, 6, 7}, {8, 9, 10, 11, 12,13,14}}; System.out.println(Arrays.deepToString(array));

    The Output:

     [[1, 2, 3, 4, 5, 6, 7], [8, 9, 10, 11, 12, 13, 14]]

☻♥ Done Keep Code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM