java - Extract & Delete patterns not functioning entirely with Arraylists -
this first time asking question on here apologize mistakes in terms of form.
i working on assignment ap computer science involves generating listarray filled random ints (within range) , processing them methods remove objects either exceed or less threshold determined earlier in program. here example of code wrote along preconditions provided teacher.
/** * @param orig list of integer * @param mid int > 2 * @return new list of integer contains, in order, numbers in * orig >= mid / 2 */public static arraylist<integer> extractupper(arraylist<integer> orig, int mid) { arraylist<integer> myarray = new arraylist<integer>(); for(integer a: orig) { if (a >= mid/2) myarray.add(a); } return myarray; } /** * @param orig list of integer * @param mid int > 2 * @return none postcondition: numbers less mid / 2 have been * removed orig */ public static void deleteupper(arraylist<integer> orig, int mid) { for(int j = 0; j < orig.size(); j++) { if (orig.get(j) >= (mid/2)) orig.remove(j); } }
to me, seems should work fine, when run this:
ic static void main(string[] args) { //a. int listlen = 10; int listmax = 20; system.out.println("listlen equals " + listlen + " , listmax equals " + listmax); system.out.println(); //b. system.out.println("generating fixed-length arraylist of length " + listlen + " values >= 0 , < " + listmax); arraylist<integer> array1 = main.buildfixedlist(listlen, listmax); system.out.println(array1); //c. system.out.print("the numbers in arraylist >= " + listmax/2 + " are: "); arraylist<integer> array2 = main.extractupper(array1, listmax); system.out.println(array2); //d. system.out.print("after deleting numbers > " + listmax/2 + " modified list is: "); main.deleteupper(array1, listmax); system.out.println(array1); //e. system.out.print("after deletion, numbers in list >= " + listmax/2 + " are: "); arraylist<integer> array3 = main.extractupper(array1, listmax); system.out.println(array3); //f. system.out.println();
my output seems ignore numbers, more others.
listlen equals 10 , listmax equals 20 generating fixed-length arraylist of length 10 values >= 0 , < 20 [14, 16, 12, 9, 8, 11, 14, 16, 1] numbers in arraylist >= 10 are: [14, 16, 12, 11, 14, 16] after deleting numbers > 10 modified list is: [16, 9, 8, 14, 1] after deletion, numbers in list >= 10 are: [16, 14]
the >=10 , <10 methods work occasionally, figure it's more of crap-shoot right now. in particular example >=10 method worked <10 did not. @ loss wrong code.
edit: thank replies, appreciate help. have edited both extractupper , deleteupper methods , getting higher rate of success, code seems ignore numbers. here's code:
/** * @param orig list of integer * @param mid int > 2 * @return new list of integer contains, in order, numbers in * orig >= mid / 2 */public static arraylist<integer> extractupper(arraylist<integer> orig, int mid) { arraylist<integer> myarray = new arraylist<integer>(); (int = 0; < orig.size(); i++){ if(orig.get(i) >= mid/2) { myarray.add(orig.get(i)); } } return myarray; } /** * @param orig list of integer * @param mid int > 2 * @return none postcondition: numbers less mid / 2 have been * removed orig */ public static void deleteupper(arraylist<integer> orig, int mid) { ( int = orig.size()-1; >= 0; i--){ if (i < orig.size()) { if(orig.get(i) >= mid/2) { orig.remove(i); i++; } } else i--; } }
here few outputs directly program:
listlen equals 10 , listmax equals 20 generating fixed-length arraylist of length 10 values >= 0 , < 20 [4, 15, 8, 11, 18, 16, 7, 3, 6] numbers in arraylist >= 10 are: [15, 11, 18, 16] after deleting numbers > 10 modified list is: [4, 8, 7, 3, 6] after deletion, numbers in list >= 10 are: [] generating fixed-length arraylist of length 10 values >= 0 , < 20 [6, 3, 9, 16, 4, 4, 17, 8, 4] numbers in arraylist >= 10 are: [] after deleting numbers > 10 modified list is: [6, 3, 9, 4, 4, 8, 4] after deletion, numbers in list >= 10 are: [] listlen equals 10 , listmax equals 20 generating fixed-length arraylist of length 10 values >= 0 , < 20 [4, 5, 0, 4, 12, 12, 1, 12, 10] numbers in arraylist >= 10 are: [12, 12, 12, 10] after deleting numbers > 10 modified list is: [4, 5, 0, 4, 1, 12] after deletion, numbers in list >= 10 are: [12] generating fixed-length arraylist of length 10 values >= 0 , < 20 [15, 16, 2, 8, 1, 7, 3, 0, 15] numbers in arraylist >= 10 are: [12] after deleting numbers > 10 modified list is: [2, 8, 1, 7, 3, 0] after deletion, numbers in list >= 10 are: [12]
the problem in deleteupper
method.
when delete item list, indexes in list change - if remove item 3, item accesible @ index 4 becomes number 3.
in implementation increase index pointer, regardless if deletion happened or not. means if 2 consecutive items meet deletion criterion, first 1 removed.
use iterator instead:
iterator<integer> = orig.iterator(); while (i.hasnext()) { if (i.next() >= mid) { i.remove(); } }
if don't want use iterator:
for (int i=0; i<orig.size(); ) { if (orig.get(i) >= mid) { orig.remove(i); } else { i++; } }
Comments
Post a Comment