Solved

Hints on how to replace an item in a linked list

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 196
servlet doXXX methods 3 50
table example 4 29
Facing this issue for maven proxy setting 2 13
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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

832 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