Created
September 10, 2016 03:56
-
-
Save dested/97aa4b574b58fc8da898e3efb1f1809d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cse360assign1; | |
import java.util.*; | |
public class stringList { | |
private ArrayList<String> list; | |
private int count; | |
boolean DEBUG = true; | |
stringList() { | |
list = new ArrayList<>(10); | |
for(int index = 0; index < 10; index++){ | |
list.add(""); | |
} | |
} | |
public void insert(String element) { | |
if (!list.contains(element)) { | |
if (count == 0) { | |
list.add(0, element); | |
count++; | |
if (DEBUG) | |
System.out.println("new element is: " + element); | |
} else { | |
int cursor = 0; | |
for (int index = 0; index < count; index++, cursor++) { | |
if(element.compareTo(list.get(index)) < 0) { | |
if (DEBUG) | |
System.out.println("new element is less than previous element"); | |
//here | |
for (int index2 = count; index2 > cursor; index2--) { | |
list.set(index2, list.get(index2 - 1)); | |
} | |
list.set(cursor, element); | |
count++; | |
if (DEBUG) | |
System.out.println("new element is: " + element); | |
} | |
//here^^ | |
} | |
} | |
} | |
} | |
public String toString() { | |
if (DEBUG) | |
System.out.println("printing element"); | |
String string = ""; | |
for (int index = 0; index < list.size(); index++ ) | |
string += list.get(index) + " "; | |
return string; | |
} | |
public int search(String element) { | |
if (DEBUG) | |
System.out.println("searching for element"); | |
for(int print = 0; print < count; print++) | |
if (list.get(print).equals(element)) | |
return print; | |
return -1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment