• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 148
  • Last Modified:

output a set or a list

Hello experts,

 when I try print a set or a list:

   System.out.println(set1);
   System.out.println(list1);

 the output looks like:

  [A, B, C]
------------------------
 is there any other way to print it ? I want the output look like:
   1.A
   2.B
   3.C
0
meow00
Asked:
meow00
2 Solutions
 
objectsCommented:
Iterator i = set1.iterator();
int n = 1;
while(i.hasNext()) {
   System.out.println(n+"."+i.next());
   n++;
}
0
 
Mayank SAssociate Director - Product EngineeringCommented:
For a list you can use a loop:

for ( int i = 0, count = list.size () ; i < count ; i ++ )
 System.out.println ( ( i + 1 ) + "." + list.get ( i ) ) ;
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now