• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

arraylist

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.util.*;
class ArrayList{
public static void main(String ar[]){
ArrayList y = new ArrayList();
y.add(2);
y.add("5");
y.add("7");
for(Object o:y){
System.out.println(y);
}
}
}
here the output is
[2, 5, 7]
[2, 5, 7]
[2, 5, 7]
howcome this happen. it should be 2,5,7
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Are you able to compile this? Because it is giving compile time error.
Any way, the reason of your answer is, instead of printing items of ArrayList, you are printing ArrayList it self.
---------------------------
for(Object o:y){
System.out.println(y);
}
---------------------------

you should write

for(Object o:y){
System.out.println(o);
}
 
this llama doesn't want your drama, he just wants this tiny ad for his mama
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic