Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to store a name in an arraylist?

 
Russel Crowe
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai guys, iam learning java at the moment, and I got problem on arraylist.

Iam asked to do a store a name of a person, Ben, Luke, Hawk, and their age in to an arraylist and display it vertically, Suppose to be like this:

Name Age

Ben 15
Luke 14
Hawk 19

Here is what i done in main.java:
ArrayList<String> names = new ArrayList<String>();
names.add("Ben");
names.add("Luke");
names.add("Hawk");
System.out.println(names);

but it prints horizontaly, and the age will also too.
Can anybody have time to share the clue?

And 1 more think, we do this in main.java or in class.java ? iam waiting for reply^^

 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're printing the entire list, not each name. In order to print the age you have to store it somewhere.
 
Russel Crowe
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm..so I should store all the 3 names in class.java and later call it in main.java right?

Can you give me a clue to do it? Thanks for your replay
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, keeping the name and age in a separate class is probably a good idea--what have you tried so far?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic