Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Hints on how to replace an item in a linked list

Posted on 2006-11-21
3
Medium Priority
?
202 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 500 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

916 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