1

I have a class MP3Track with members: Track number (int), track name (String), artist String), in faourites (boolean) etc. My catalogue class contains two ArrayLists, one containing all tracks (vectorMain) and the other containing fovuorites (vectorFav) which are copies of the tracks in the main catalogue.

I want the user to have the option of swapping two track numbers (which are unique in both arrays).

I have added the following code to do this and it works (if they exist - ATM) but it seems rather cumbersome. Surly there must be a better solution?

getMainIndex() - returns the index position in the ArrayList for a given track number or -1.

public void swapTrack(){
    int t1 = -1, t2 = -1;
    System.out.println(face.getSwapTrackMenu());       
    System.out.print("1) Please enter first track number to swap: ");
    t1 = scan.readInt();                                
    System.out.print("2) Please enter second track number to swap: ");
    t2 = scan.readInt();                                

    if((getMainIndex(t1)!=-1)&&(getMainIndex(t2)!=-1)){
        String s1 = null;
        String s2 = null;
        for(MP3Track track : vectorMain){
            if(track.getTrackNo() == t1){
                s1 = track.getTitle();
            }
            if(track.getTrackNo() == t2){
                s2 = track.getTitle();
            }
        }

        for(MP3Track track : vectorMain){
            if(track.getTitle().equals(s1)){
                track.setTrackNo(t2);
            }
            if(track.getTitle().equals(s2)){
                 track.setTrackNo(t1);
            }
        }

        for(MP3Track track : vectorFav){
            if(track.getTitle().equals(s1)){
                track.setTrackNo(t2);
            }
            if(track.getTitle().equals(s2)){
                track.setTrackNo(t1);
            }
        }
    }
}//End moveTrack

I'm new to Java so any suggestions, improvements & comments appreciated.

Many thanks

Ok well I've also tried the following which seems more elegant and works??? But surly there is a better way to achieve this?? What am I missing? I've hit a brick wall with it here!

    for(MP3Track track : vectorMain){
        if(track.getTrackNo() == t1){
            mp31 = vectorMain.get(getMainIndex(t1));
            //mp31.setTrackNo(t2);
        }
        if(track.getTrackNo() == t2){
            mp32 = vectorMain.get(getMainIndex(t2));
            //mp32.setTrackNo(t1);
        }
    }
    mp31.setTrackNo(t2);
    mp32.setTrackNo(t1);
    vectorMain.add(vectorMain.remove(getMainIndex(t1)));
    vectorMain.add(vectorMain.remove(getMainIndex(t2)));

    for(MP3Track track : vectorFav){
        if(track.getTrackNo() == t1){
            mp31 = vectorFav.get(getFavIndex(t1));
        }
        if(track.getTrackNo() == t2){
            mp32 = vectorFav.get(getFavIndex(t2));
        }
    }
    mp31.setTrackNo(t2);
    mp32.setTrackNo(t1);
    vectorFav.add(vectorFav.remove(getFavIndex(t1)));
    vectorFav.add(vectorFav.remove(getFavIndex(t2)));

Many thanks

1 Answer 1

2

You should use "indexOf", it's a method on an ArrayList to retrieve the index of an object. I think you know how to go from there. http://docs.oracle.com/javase/1.4.2/docs/api/java/util/ArrayList.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.