**** these LinkedLists!

private Customers c;

private LinkedList<Bin> stock;

private Iterator<Bin> it;

private PlumbersDream pD;

public NextFit(PlumbersDream pD, Customers c)

{

this.pD = pD;

this.c = c;

stock = pD.getStock();

it = stock.iterator();

}

publicvoid init(Pipe pipe)

{

for(Iterator<Bin> i = it; i.hasNext();)

{

Bin b = i.next();

if (b.getCapacity() >= pipe.getLength())

{

b.cut(pipe);

if (b.getCapacity() < 5)

{

i.remove();

}

break;

}

if( ! i.hasNext() )

{

pD.newStock();

stock.getLast().cut(pipe);

it.next();

break;

}

}

}

All I want to do is make it so that when a new element is added to the end of the list, the for loop starts iterating from this element onwards and not from the beginning.

Using arrays I did something like:

for (int i = count; i < stock.length; i++)

where count was a global int variable that I added one to everytime a new element was added. What I have for the LinkedList doesn't appear to be working.

On the first init of the method the if statement is satisfied and so the code within the statement is carried out. The program now breaks out of the for loop. On the second init, the if statement should again be satisfied by my test data, but the code isn't getting executed.

Any ideas?

[2286 byte] By [frances_farmera] at [2007-10-2 16:10:21]
# 1
someone must know. all I want to do is iterate from a given element rather than from the beginning.
frances_farmera at 2007-7-13 16:54:19 > top of Java-index,Java Essentials,Java Programming...
# 2
http://java.sun.com/j2se/1.5.0/docs/api/java/util/List.html#listIterator(int) http://java.sun.com/j2se/1.5.0/docs/api/java/util/LinkedList.html#listIterator(int)?
Lokoa at 2007-7-13 16:54:19 > top of Java-index,Java Essentials,Java Programming...
# 3

Maybe you should look more closely at LinkedList's docs. If I understand your problem correctly, there's a method that does exactly what you want.

http://java.sun.com/j2se/1.5.0/docs/api/java/util/LinkedList.html

However, LL is not the best choice for this kind of thing. If your list is small, and you won't be doing this frequently relative to other processing in your program, you probably won't notice a problem, but for indexed access, LL is much less efficient than ArrayList.

jverda at 2007-7-13 16:54:19 > top of Java-index,Java Essentials,Java Programming...
# 4
Okay, copying those links into your browser will work properly...
Lokoa at 2007-7-13 16:54:19 > top of Java-index,Java Essentials,Java Programming...
# 5

> Okay, copying those links into your browser will work

> properly...

Or clicking them... :-P

[url http://java.sun.com/j2se/1.5.0/docs/api/java/util/List.html#listIterator(int)]http://java.sun.com/j2se/1.5.0/docs/api/java/util/List.html#listIterator(int)[/url]

[url http://java.sun.com/j2se/1.5.0/docs/api/java/util/LinkedList.html#listIterator(int)]http://java.sun.com/j2se/1.5.0/docs/api/java/util/LinkedList.html#listIterator(int)[/url]

jverda at 2007-7-13 16:54:19 > top of Java-index,Java Essentials,Java Programming...
# 6

if( ! i.hasNext() )

{

pD.newStock();

stock.getLast().cut(pipe);

it.next();

break;

}

Why doesn't that work though. As that should move the "it" iterator to that element, and then the "i" iterator should then iterate from that position onwards as "it" is assigned to "i". Do you see what I mean?

Although the code is slightly different (i've adde and restrctured slightly) surely what I have should work the same as for an array:

public void init(Pipe pipe)

{

for (int i = count; i < stock.length; i++)

{

if (stock[i] == null)

{

pD.newStock(i);

stock[i].cut(pipe);

count++;

break;

}

else

{

if (stock[i].getCapacity() >= pipe.getLength())

{

stock[i].cut(pipe);

break;

}

}

}

}

frances_farmera at 2007-7-13 16:54:19 > top of Java-index,Java Essentials,Java Programming...
# 7
> > Okay, copying those links into your browser will> work> > properly...> > Or clicking them... :-P> If you use url tags, which I didn't :-)Thanks.
Lokoa at 2007-7-13 16:54:19 > top of Java-index,Java Essentials,Java Programming...
# 8

>

> if( ! i.hasNext() )

> {

>pD.newStock();

>stock.getLast().cut(pipe);

>it.next();

>break;

> }

>

>

> Why doesn't that work though. As that should move the

> "it" iterator to that element, and then the "i"

> iterator should then iterate from that position

> onwards as "it" is assigned to "i". Do you see what I

> mean?

No, I don't.

Can you post enough code to actually demonstrate the problem? Did you read our replies and change how you're getting the iterator accordingly?

jverda at 2007-7-13 16:54:19 > top of Java-index,Java Essentials,Java Programming...
# 9
Try a while loop with a counter which you monitor and reset, when it returns to your start exit the loop.
ra7a at 2007-7-13 16:54:19 > top of Java-index,Java Essentials,Java Programming...