java - Split method for unordered array list -
i trying create method named split divides list 2 lists according key. if list_1 , list_2 resulting lists, list_1 should contain items of original list keys less or equal key passed , list_2 should contain items of original list keys larger key passed. post code far , other people have suggested
public class unorderedarraylist extends arraylistclass { public unorderedarraylist() { super(); } public unorderedarraylist(int size) { super(size); } //bubble sort public void bubblesort() { (int pass = 0; pass < length - 1; pass++) { (int = 0; < length - 1; i++) { if (list[i] > list[i + 1]) { int temp = list[i]; list[i] = list[i + 1]; list[i + 1] = temp; } } } } //implementation abstract methods defined in arraylistclass //unordered list --> linear search public int search(int searchitem) { for(int = 0; < length; i++) if(list[i] == searchitem) return i; return -1; } public void insertat(int location, int insertitem) { if (location < 0 || location >= maxsize) system.err.println("the position of item inserted out of range."); else if (length >= maxsize) system.err.println("cannot insert in full list."); else { (int = length; > location; i--) list[i] = list[i - 1]; //shift right list[location] = insertitem; length++; } } public void insertend(int insertitem) { if (length >= maxsize) system.err.println("cannot insert in full list."); else { list[length] = insertitem; length++; } } public void replaceat(int location, int repitem) { if (location < 0 || location >= length) system.err.println("the location of item replaced out of range."); else list[location] = repitem; } public void remove(int removeitem) { int i; if (length == 0) system.err.println("cannot delete empty list."); else { = search(removeitem); if (i != -1) removeat(i); else system.out.println("cannot delete! item deleted not in list."); } } public void merge(unorderedarraylist list2,unorderedarraylist list1){ int num=0; for(int j=0; j<list1.length;j++){ num= list1.retrieveat(j); insertend(num); } for(int i=0; i<list2.length-1;i++){ num=list2.retrieveat(i); insertend(num); } } public void split(unorderedarraylist list2, unorderedarraylist list1, unorderedarraylist list, int item){ int listitem = item; while(!list.isempty()){ list.retrieveat(listitem); if(listitem>item){ if(!list2.isfull()){ list2.insertat(listitem); } } } } //what got far internet /* void unsortedtype::splitlists(itemtype item, unsortedtype& list1, unsortedtype& list2){ itemtype listitem; list.resetlist(); while ( !list.islastitem()) { list.getnextitem(listitem); if(listitem > item) { if (!list2.isfull()) list2.insertitem(listitem); } else { if ( !list1.isfull()) list1.insertitem(listitem); } }} */
you have lot of code, unrelated requirement. given list , key, simply:
- create 2 result lists
- iterate through original list, copying element 1 of result lists depending on value relative key
- return 2 result lists
that's need.
Comments
Post a Comment