Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Hints on how to replace an item in a linked list

Posted on 2006-11-21
3
Medium Priority
?
201 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.
Suggested Courses

730 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