ajsie
ajsie

Reputation: 79826

print arraylist element?

how do i print the element "e" in arraylist "list" out?

 ArrayList<Dog> list = new ArrayList<Dog>();
 Dog e = new Dog();
 list.add(e);
 System.out.println(list);

Upvotes: 13

Views: 192297

Answers (7)

Prabhashani
Prabhashani

Reputation: 99

If you want to print an arraylist with integer numbers, as an example you can use below code.

class Test{
    public static void main(String[] args){
        ArrayList<Integer> arraylist = new ArrayList<Integer>();

        for(int i=0; i<=10; i++){
            arraylist .add(i);
        }
       for (Integer n : arraylist ){
            System.out.println(n);
       }
   }
}

The output is above code:

0
1
2
3
4
5
6
7
8
9
10

Upvotes: 0

Summved Jain
Summved Jain

Reputation: 890

Printing a specific element is

list.get(INDEX)

I think the best way to print the whole list in one go and this will also avoid putting a loop

Arrays.toString(list.toArray())

Upvotes: 0

AJNeufeld
AJNeufeld

Reputation: 8705

Here is an updated solution for Java8, using lambdas and streams:

System.out.println(list.stream()
                       .map(Object::toString)
                       .collect(Collectors.joining("\n")));

Or, without joining the list into one large string:

list.stream().forEach(System.out::println);

Upvotes: 4

sateesh
sateesh

Reputation: 28713

Do you want to print the entire list or you want to iterate through each element of the list? Either way to print anything meaningful your Dog class need to override the toString() method (as mentioned in other answers) from the Object class to return a valid result.

public class Print {
    public static void main(final String[] args) {
        List<Dog> list = new ArrayList<Dog>();
        Dog e = new Dog("Tommy");
        list.add(e);
        list.add(new Dog("tiger"));
        System.out.println(list);
        for(Dog d:list) {
            System.out.println(d);
            // prints [Tommy, tiger]
        }
    }

    private static class Dog {
        private final String name;
        public Dog(final String name) {
            this.name = name;
        }

        @Override
        public String toString() {
            return name;
        }
    }
}

The output of this code is:

[Tommy, tiger]  
Tommy  
tiger

Upvotes: 12

GuruKulki
GuruKulki

Reputation: 26428

You should override toString() method in your Dog class. which will be called when you use this object in sysout.

Upvotes: 0

Jack
Jack

Reputation: 133639

First make sure that Dog class implements the method public String toString() then use

System.out.println(list.get(index))

where index is the position inside the list. Of course since you provide your implementation you can decide how dog prints itself.

Upvotes: 6

Topher Fangio
Topher Fangio

Reputation: 20687

Your code requires that the Dog class has overridden the toString() method so that it knows how to print itself out. Otherwise, your code looks correct.

Upvotes: 2

Related Questions