Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Array List  RSS feed

 
Ryan Newcombe
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My professor wants us to display "The first letter" in a group of 4 elements in an ArrayList. I can get the program to display the 4 elements but I can not find a method to grab the first character in an ArrayList. Please Help.

ArrayList list = new ArrayList();
list.add(new String ("Happy"));
list.add(new String ("Sleepy"));
list.add(new String ("Sneezy"));
list.add(new String ("Doc"));
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(charAt(0)));
}
}System.out.println(list.get(charAt(0)));
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is "the first letter" of a list? The first letter of the first element? Then you need to get the first element, and then get the first letter of that. If it's something else, let us know.
 
Ryan Newcombe
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You got it right. I just don't know how to grab the first character of each element.

ArrayList list = new ArrayList();
list.add("Happy");
list.add("Sleepy");
list.add("Sneezy");
list.add("Doc");
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ryan Newcombe:
You got it right. I just don't know how to grab the first character of each element.


Try...



Henry
 
Ryan Newcombe
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much! I got it. I was trying to use the charAt but I guess I just had my () wrong. Thanks again!
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!