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

Updating Linked List  RSS feed

 
Kiera Hakikawa
Greenhorn
Posts: 14
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have an assignment that asks to create a linked list of objects and then update them. I'm having a bit of trouble with the updating part.
My program prints the list twice, once before and once after it has been updated however they both currently print the same list. I have debugged my program and found the reason why is because the for loop I have to update the list is not being initiated (is that the right word?) and I was hoping a new set of eyes could see what I clearly am not. I'll be glad to expand on anything I haven't made clear. Thank you in advance.

 
Carey Brown
Bartender
Posts: 2994
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This should have thrown an exception. Let's say you had 10 trees in the list: size would be 10, and the index of the last tree would be 9 (indexes are zero based). Should have thrown an out of bounds exception.>
 
Kiera Hakikawa
Greenhorn
Posts: 14
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I changed that section to


and it works as it should.
I believe by setting int i = 1 and not = 0 it doesn't throw an exception. It was giving me weird outputs when I had it set to 0 but never threw an exception.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kiera Hakikawa wrote:'ll be glad to expand on anything I haven't made clear.

Very little. And thank you for a nicely produced, well thought-out question. Have a cow.

Two things:

1. LinkedLists aren't like an ArrayList.   Specifically, they don't implement RandomAccess, which means that methods like get(n) and remove(n) take time proportional to n. For this reason, it's more usual to add and remove elements via their ListIterator, which you can use as a "cursor".

2. All Lists (and ListIterators) have a set() method, which replaces a value, without having to add and remove.

HIH

Winston
 
Piet Souris
Rancher
Posts: 1979
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Kiera,

first: the cause of your initial listing to fail was that 'list.size()' is equal to 0 in this part:

At this point in time you had not added anything to your list. Therefore, the test 'i < size' failed at once.

Now, in your second listing, you more or less do the exact same thing, yet now you state that it works.
Your second listing does not show it, but have you added elements to your list before getting into that code?

Then, are your 'Trees' objects mutable, i.e. can you change the fields 'upperCaseName', 'minus10Height' and 'add10Diameter'?
If so, then you do not need to delete a Trees, create a new one and then add it again to your list.
Just update the fields and that is all.

If these objects are immutable, then why would you delete the 'old' ones?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!