2

Develop a method to swap two elements with specified valid indexes I and j:

public void swap(int I, int j);

This is the method I have to use to swap two of the elements in an array list. The original list is: [a ==> b ==> c ==> d............] After calling swap(0, 2): [c ==> b ==> a ==> d............] This is what should print out for the output.

--------- First class-----

package it179.pa2;

import java.util.*;

public class MyArrayList {

private String list;


MyArrayList(String list) {
    this.list = list;
}
MyArrayList() {

}

public void swap(int i, int j) {
    int temp1;
    temp1 = i;
    i = j;
    j = temp1;               
    }

@Override
public String toString() {
    return list + "==>";
}

}

----- Second Class-----

package it179.pa2;
import java.util.*;

public class MyArrayListTest {


public static final void main (String[]args) {

    MyArrayList my = new MyArrayList();


    ArrayList<MyArrayList> al = new ArrayList<MyArrayList>();

    al.add(new MyArrayList("A"));
    al.add(new MyArrayList("B"));
    al.add(new MyArrayList("C"));
    al.add(new MyArrayList("D"));
    al.add(new MyArrayList("E"));
    al.add(new MyArrayList("F"));
    System.out.print("The original list is: ");
            for (MyArrayList tmp: al) {

        System.out.print(tmp);      
    }// end of for

    System.out.println("After calling swap(0,2): ");
    System.out.print(al);
}

}

12
  • You've posted requirements and code, but have not explained the code nor asked a specific question. Please fill in the blanks. Commented Oct 18, 2016 at 21:37
  • The swap method is suppose to be called into the test class and use the method swap for the array list that I have instantiated and swap element (0,2). Commented Oct 18, 2016 at 21:42
  • care to explain what's going wrong? Commented Oct 18, 2016 at 21:43
  • First program just overrides the toString method so when the program outputs it shows that the first elements is pointing at the next with "==>" showing that. It also is suppose to house the swap method that I have to call into the second program. The 2nd program is suppose to have an Arraylist and use the swap method from the first class to override its elements and output what needs to be swapped which is (0,2) Commented Oct 18, 2016 at 21:44
  • 3
    Your swap method is flipping the integer values of the parameters, e.g. if you call swap(1, 3), you'll initially have i = 1, j = 3, and at the end you'll have i = 3, j = 1. None of that touched the list, so why would you expect the list to have changed? Of course, it's so much worse than that: 1) You don't even call swap. 2) Why does MyArrayList have a field named list of type String? That's not list. 3) None of that has access to al, so how do you expect the actual list to be updated? Commented Oct 18, 2016 at 21:52

2 Answers 2

2

Here it is with a static method:

public static <E> void swap(List<E> list, int i, int j) {
    E e = list.get(i);
    list.set(i, list.get(j));
    list.set(j, e);
}

See this question. To do this using a class, the logic would be the same, but the method signature would instead be public void swap(int i, int j).

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

2 Comments

That is basically Collections.swap method. Im not able to change the method.
public void swap(int I, int j); is the method I have to use, I can't use any other method. If I was able to use the method you posted it'd work out perfectly.
2

For swap elements in List you can you swap() method of java.util.Collections

Collections.swap(list, element1, element2)

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.