• Post Reply Bookmark Topic Watch Topic
  • New Topic

ArrayList element discrimination  RSS feed

 
Andrew X. Thompson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone! I recently enrolled in Teach Yourself Java 101 , and I’ve come across a peculiar problem. Can anyone help me shed some of this green and get in the game?

I’m trying to pick out only those strings in an arraylist of strings that are five characters or less in length. I know it has to be something simple that I'm not picking up on.
I have this so far, but that only yields s. How do I get inside the arraylist and into those strings, yet still return an ArrayList<String>?!


 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should first define your problem a bit more clearly: what do you mean by "pick out"? Do you mean you want to return a List<String> containing only five or less characters? I don't see any code that puts strings *in* to the list--does that happen elsewhere? Have you defined a list to hold the strings you select?

On a side note, unless there's a compelling reason to do so, we should declare our variables using the least-restrictive type possible--in this case a List<String> or even Collection<String>, and instantiate a specific implementation (here an ArrayList<String>).
 
Andrew X. Thompson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that is what I'd like to return and I can add the strings from elsewhere. Sorry, here's where it started,
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then for a start you'll probably want to iterate over the strings you're passing in, not the empty list of strings created in the method!
 
Andrew X. Thompson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I do pass in some strings I'm still getting an empty list s, even though I'm trying to find a way of asking for only those less than/equal to five characters in length. Is there some notation I'm missing to refer my for statement to?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you're never adding anything to the new list--you just return as soon as you hit the first string in the input array.

The idea is to loop over the *entire* input array, adding strings to the return list when appropriate, and *then* return the new list.
 
Andrew X. Thompson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks David! I'm going in a new direction now.

How do I do that...as in, how do I 1) loop through the entire array (I thought that's what my enhanced for loop was doing), and 2) how do I create a place for new strings to be added, since I can't know what strings (or how many) might come into play. What does a new list look like? Obviously I'm only returning the old one now.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your original code looked like this (with indentation and clarity changes):Hopefully it's clear why you're looping over the List<String> and not a parameter you're passing in. If it's not, you should probably take a step back and review some pretty basic Java stuff regarding variable declaration, parameters, and the like.

If the entire method looks like this (with minor type changes):There are (at least) two changes necessary:
(1) Loop over the *entire* parameter (the String array) being passed in: doing this is essentially the same as you're doing now, but iterating over the parameter--not the list.
(2) Adding *each* string of the appropriate length to the list you've created: the List API documentation may help you with figuring out how to add items to a list, but I'd be awfully surprised if whatever source you're using to learn Java didn't cover some List basics, like how to add items to lists.
 
Michael Angstadt
Ranch Hand
Posts: 278
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrew Thompson wrote:Thanks David! I'm going in a new direction now.

How do I do that...as in, how do I 1) loop through the entire array (I thought that's what my enhanced for loop was doing), and 2) how do I create a place for new strings to be added, since I can't know what strings (or how many) might come into play. What does a new list look like? Obviously I'm only returning the old one now.


Your input array is what has all the words in it, so you need to iterator over that, NOT the list you are returning:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!