Reputation: 33
I'm not allowed to import anything so java.util.arrays/Import lists can't be used.
So far I've got this:
public class MyArrayList {
public String[] arrays;
int counter = 0;
public MyArrayList() {
arrays = new String[5];
}
public void add(String element) {
if (counter >= arrays.length) {
String[] newarray = new String[arrays.length + 1];
for (int i = 0; i < arrays.length; i++) {
newarray[i] = arrays[i];
}
arrays = newarray;
}
arrays[counter] = element;
counter++;
}
public void set(String element, int position) { //This is the code that will change pos
element = arrays[arrays.length - 1];
for (int y = arrays.length - 1; y > position; y--)
arrays[y] = arrays[y - 1];
arrays[position] = element;
}
public class MyArrayListTest {
static MyArrayList zoo = new MyArrayList();
public static void printZoo() {
System.out.print("The zoo now holds " + zoo.size() + " animals: ");
for (int j = 0; j < zoo.size(); j++) System.out.print(zoo.get(j) + " ");
System.out.println();
}
public static void main(String[] args) {
System.out.println("Testing constructor, add(object) and size() ");
zoo.add("Ant");
zoo.add("Bison");
zoo.add("Camel");
zoo.add("Dog");
zoo.add("Elephant");
zoo.add("Frog");
zoo.add("Giraffe");
zoo.add("Horse");
printZoo();
zoo.set("Frog",0);
zoo.set("Camel",2);
zoo.set("Dog",4);
zoo.set("Elephant",6);
zoo.set("Giraffe",7);
zoo.set("Horse",5);
zoo.set("Ant",1);
zoo.set("Bison",3);
printZoo();
System.out.println();
So what I want to do is make the set method change the positions of elements in the array.
This is what gets printed out:
The zoo now holds 8 animals: Ant Bison Camel Dog Elephant Frog Giraffe Horse
The zoo now holds 8 animals: Horse Elephant Ant Giraffe Bison Frog Dog Camel
So locations do change but it's not in the right order.
It SHOULD be :
The zoo now holds 8 animals: Frog Ant Camel Bison Dog Horse Elephant Giraffe
Upvotes: 0
Views: 6410
Reputation: 927
Why are you overriding the value of element in set function? You have no variable which holds the value of wanted property then. Look at your code again.
public void set(String element, int position) {
element = arrays[arrays.length - 1]; // at this point, you no longer
// know what the element was
//and now you shift the list by 1 element
for (int y = arrays.length - 1; y > position; y--)
arrays[y] = arrays[y - 1];
arrays[position] = element;
}
You are just shifting the list. You could try doing that in this way
public void set(String element, int position) {
String tmp = arrays[position];
arrays[position] = element;
...
... //here check if element you have set was already inside the array
... //if it isn't, increase array length and put tmp at the end
... //if it is, put tmp in old index of element
}
I mean it's just a quick answer, without thinking about corner cases but it seems to me that your idea was a little bit incorrect.
Ok, here is complete solution to your problem :)
public void set(String element, int position) {
if (arrays[position].equals(element))
return;
String tmp = arrays[position];
arrays[position] = element;
for (int i = 0; i < arrays.length; i++) {
if (arrays[i].equals(element) && i != position) {
arrays[i] = tmp;
return;
}
}
add(tmp);
}
Upvotes: 1