Last active
December 30, 2015 08:39
-
-
Save olupotd/7804005 to your computer and use it in GitHub Desktop.
Simple Data Structures and Algorithms implementation
This file contains 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
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
public class DataStructures { | |
//declared an array of | |
Object[] orderedList = {20,45,51,78,84,91,98}; | |
int[] elems = {10,15,77,92,200}; | |
List<Integer> list = new ArrayList<Integer>(); | |
Object[] addElement(Object[] orderedList2, int added) { | |
Object[] result = Arrays.copyOf(orderedList2, orderedList2.length +1); | |
result[orderedList2.length] = added; | |
return result; | |
} | |
public DataStructures(String s) { | |
//initialize the ordered list | |
for(int i = 0; i < elems.length; i++) | |
{ | |
orderedList = addElement(orderedList, elems[i]); | |
} | |
Arrays.sort(orderedList); | |
viewList(orderedList); | |
print("Without 45 and 91"); | |
delete(orderedList, 45, 91); | |
viewList(orderedList); | |
} | |
public void delete(Object[] ar, Object elem, Object elm2) | |
{ | |
for(int i = 0; i < ar.length; i++) | |
{ | |
if(ar[i] != elem || ar[i] != elm2) list.add((Integer) ar[i]); | |
} | |
orderedList = list.toArray(); | |
} | |
private void viewList(Object[] orderedList2) { | |
for(int x = 0; x<orderedList2.length; x++)print(orderedList2[x]+""); | |
} | |
public static void main(String[] args) { | |
new DataStructures("Starting..."); | |
} | |
// Prints a string parsed to it as a parameter. | |
public void print(String info) | |
{ | |
System.out.println(info); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment