Solved

Hints on how to replace an item in a linked list

Posted on 2006-11-21
3
191 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now