• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 145
  • 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

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

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