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);
}

}

Coder
  • 2,153
  • 1
  • 16
  • 21
Andrew Johnson
  • 31
  • 1
  • 1
  • 5
  • You've posted requirements and code, but have not explained the code nor asked a *specific* question. Please fill in the blanks. – Hovercraft Full Of Eels Oct 18 '16 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). – Andrew Johnson Oct 18 '16 at 21:42
  • care to explain what's going wrong? – Coder Oct 18 '16 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) – Andrew Johnson Oct 18 '16 at 21:44
  • This might be more appropriate posted in Code Review (http://codereview.stackexchange.com/) – Kristian H Oct 18 '16 at 21:45
  • The original list is: A==>B==>C==>D==>E==>F==> After calling swap(0,2): [A==>, B==>, C==>, D==>, E==>, F==>] This is the output Im getting and the first and 3rd element needs to swapped – Andrew Johnson Oct 18 '16 at 21:46
  • 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? – Andreas Oct 18 '16 at 21:52
  • I need that to be inside the list but don't know how to code it correctly to do exactly what you said. I want swap to look inside the list and look for index 0 and index 2 and swap those two elements and then printout the output. – Andrew Johnson Oct 18 '16 at 21:55
  • I have called swap but it didn't do anything so I deleted the call over to it before I posted the code. When I called it over with my.swap(0,2); it doesnt change the output at all, the list still stays the same. What would you think I should put the list type as other than String? – Andrew Johnson Oct 18 '16 at 21:59
  • How is your list a list? It just has a `String`. – Louis Wasserman Oct 18 '16 at 22:07
  • This is the first time I've done anything with a list so I thought the list was just storing a String inside the list. The list has to use the alphabet but I haven't got far enough to add the rest of the letters. How would I make my list a list and not a String? – Andrew Johnson Oct 18 '16 at 22:10
  • Possible duplicate of [Arraylist swap elements](https://stackoverflow.com/questions/15963549/arraylist-swap-elements) – Henrik Aasted Sørensen Sep 20 '17 at 08:15

2 Answers2

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).

Community
  • 1
  • 1
NcAdams
  • 2,521
  • 4
  • 21
  • 32
2

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

Collections.swap(list, element1, element2)
L.Petrosyan
  • 430
  • 4
  • 12