Solved

Hints on how to replace an item in a linked list

Posted on 2006-11-21
3
198 Views
Last Modified: 2010-03-31
This is the code I have so far:

Do I need to use a temporary variable in order to replace the tracks?

public void replaceImage(Image t, int index)
{
            Image image = firstImage;

            for(int i = 1; i <= index; i++)
            {
                  image = image.getNext(); // finds image identified by the given index i.e. the one the user wants to replace
            }

            t.setNext(image.getNext()); //this and below replaces the references, well it should do
            t.setPrevious(image.getPrevious());

            t.setImageName(t.getImageName()); // t is the track name that should overwrite image
}

Thanks anyone
0
Comment
Question by:john8932
  • 2
3 Comments
 
LVL 3

Expert Comment

by:nishayr
ID: 17992922
I would have to assume do the following after the for loop instead of the other 3 lines:

Image oldImage = t.getNext();
t.setNext(image);
image.setNext(oldImage.getNext());
image.getNext().setPrevious(image);
image.setPrevious(t);

Thanks,
Nish
0
 
LVL 3

Accepted Solution

by:
nishayr earned 125 total points
ID: 17992932
Actually, I forgot to check for a null...

Image oldImage = t.getNext();
t.setNext(image);
image.setNext(oldImage.getNext());
image.setPrevious(t);
if (image.getNext() != null)
    image.getNext().setPrevious(image);


Try that instead! 8)

Thanks,
Nish
0
 

Author Comment

by:john8932
ID: 17996192
I was only really looking for hints but thanks anyway.

Ive actually got it working now a different way.

Cheers
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
runtime exception 2 50
difference between sorce folder and folder in eclipise 3 40
maven disable workspace resolution 1 37
Running JavaFX on the Raspberry Pi 27 119
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question