Removing an item of 'n' length from an arraylist
I have been trying for 3 hours and there must be something that I am missing.
I can get it to remove something from the spot lets say 3, but I cannot get it to remove all items in the list that are of the length 3. Anyone have an idea of what I may be doing wrong? Can I use something like:
myList.remove(myList.length(3)); ?
I jsut cant get it.
[370 byte] By [
Soul40ka] at [2007-10-2 6:24:46]

This is why I don't think learning Java as a first language is good. If you knew C, this would make a lot more sense
I didn't have much choice since I am learning this in a school class :D Tryin my hardest with what I have (i have suggested C to him but he cant do anything abou ***)
No sry not trying to criticize you.Try this: Instead of using a List, use an array. Iterate through all the elements, and when you find one that's of length 3, remove it. This can be accomplished by moving everything after it 1 position up.
you wouldnt need to literally move everything up if you are using an Iterator..
List array = new ArrayList<String>();
array.add("string 1");
array.add( "boo");
Iterator<String> itr = array.iterator();
while(itr.hasNext())
{
if(itr.next().length() >= 3)
itr.remove();
}
Something like that will work..
Hehe, I didn't take it as critism more of sarcasm if it can even be used there :P
I understand what your saying but like usual I have a problem translating that into code.
What I think your saying is something like:
for (int i = 0 ; i < myList.size(); i++)
{
if (myList.get(i) = length(3))
myList.remove(i);
}
?
Hmm, Iterators are still slightly new to me so I wouldn't have thought of this.Thank you though, i see it and understand what it is saying. Hopefully it turns out to be what was needed.
> Hehe, I didn't take it as critism more of sarcasm if
> it can even be used there :P
>
> I understand what your saying but like usual I have a
> problem translating that into code.
>
> What I think your saying is something like:
>
> > for (int i = 0 ; i < myList.size(); i++)
> {
>if (myList.get(i) = length(3))
>myList.remove(i);
> }
>
Apparently your instructor is not teaching you very well...
length() is a method used by arrays or strings etc and has NO variables..
To correct your code:
for (int i = 0 ; i < myList.size(); i++)
{
if (myList.get(i).length() == 3)
myList.remove(i);
}
that will only work if the object your are referencing is a String though.. or some other type that has a length..
hmm I cant seem to get it to work.
In #7, your list size keeps changing. So, your 'for' loop termination condition will keep changing.Try stepping through the loop backwards.See Reply #2 here: http://forum.java.sun.com/thread.jspa?threadID=622656
MLRona at 2007-7-16 13:26:37 >

I tried
for (int i = 0 ; i < myList.size () ; i++)
{
if (myList.get (i).length () == 3)
myList.remove (i);
AND
String x = (String) myList.get (i);
if (x.length () == len)
myList.remove (i);
}
It isn't sinking in and I cant figure out Iterators enough to get that code he gave me to work :(
As I said, see reply #7, but do the loop backwards:for (int i=myList.size()-1; i>=0; i--)And see the link that I gave you to see why you need to go backwards.
MLRona at 2007-7-16 13:26:37 >

Ok, I get it now why I would need it backwards. But for
for (int i = myList.size () - 1 ; i >= 0 ; i--)
{
if (myList.get (i).length () == 3)
myList.remove (i);
}
(#7 was my post so i figured it was the next one)
it says "No method named "length" was found in "java.lang.Object".
Which I do believe just means I cant use length that way.
I havent read this entire thread, but Im assuming myList exclusively holds String objects. THe problem is you need to cast the object from the list before you use the length() method (because String has lenght(), not Object).
so adjust your loop accordingly:
String s;
for (int i = myList.size()-1 ;i >= 0 ;i--) {
s = (String)myList.get(i);
if (s.length() == 3)
myList.remove(i);
}
}
ahhh i just forgot to cast it. thank you.
> (#7 was my post so i figured it was the next one)I'm glad you figured it out, but #7 was by DarkNomad, not by you.
MLRona at 2007-7-20 18:55:30 >

casting sucks.. should useArrayList myList = new ArrayList<String>();casting gets annoying.. and takes up space
ArrayList myList = new ArrayList<String>();does not work for me.
for (int i = myList.size () - 1 ; i >= 0 ; i--)
{
String s = (String) myList.get (i);
if (s.length () == len)
myList.remove (i);
}
worked anyways.
Thank you everyone for lending many different ways to do this. I came out knowing more than I thought I would get out of it.
> casting sucks.. should use
>
> ArrayList myList = new ArrayList<String>();
>
> casting gets annoying.. and takes up space
So does declaring a concrete type unnecessarily.
List<String> myList = new ArrayList<String>();
That's better. Although I would advise the original poster to ignore this. Introducing generics to you when you're having trouble with much more fundamental and simple concepts probably isn't going to serve you well.