java - How to remove a certain number from an array? -
my code far supposed 1 thing, supposed exclude "0" array , not sure how implement code , exclude number 0 when counts positive (pozitivne) numbers in code.
this code:
integer[] array = {12, 23, -22, 0, 43, 545, -4, -55, 43, 12, 0, -999, -87, 12}; int pos2 = 0, neg2 = 0; (int : array) { if (i >= 0) { pos2++; } else { neg2++; } } p1 = new int[pos2]; n1 = new int[neg2]; pos2 = 0; neg2 = 0; (int : array) { if (i >= 0) { p1[pos2] = i; pos2++; } else { n1[neg2] = i; neg2++; } } system.out.print("ukupno: "); (int : array) { system.out.print(" " + i); } system.out.print("\npozitivni: "); (int : p1) { system.out.print(" " + i); } system.out.print("\nnegativni: "); (int : n1) { system.out.print(" " + i); }}}
you need 2 conditions here 1 positives negatives. if remove "=" condition (i >= 0) '0' go negative's array.
integer[] array = {12, 23, -22, 0, 43, 545, -4, -55, 43, 12, 0, -999, -87, 12}; int pos2 = 0, neg2 = 0; (int : array) { if (i > 0) { pos2++; } else if(i < 0){ neg2++; } } p1 = new int[pos2]; n1 = new int[neg2]; pos2 = 0; neg2 = 0; (int : array) { if (i > 0) { p1[pos2] = i; pos2++; } else if(i < 0){ n1[neg2] = i; neg2++; } } system.out.print("ukupno: "); (int : array) { system.out.print(" " + i); } system.out.print("\npozitivni: "); (int : p1) { system.out.print(" " + i); } system.out.print("\nnegativni: "); (int : n1) { system.out.print(" " + i); }}} same result using collections, 1 loop required filter positives , negatives.
integer[] array = {12, 23, -22, 0, 43, 545, -4, -55, 43, 12, 0, -999, -87, 12}; list<integer> pos1 = new arraylist<>(); list<integer> neg1 = new arraylist<>(); (int : array) { if (i > 0) { pos1.add(i); } else if(i < 0){ neg1.add(i); } } system.out.print("ukupno: "); (int : array) { system.out.print(" " + i); } system.out.print("\npozitivni: "); (int : pos1) { system.out.print(" " + i); } system.out.print("\nnegativni: "); (int : neg1) { system.out.print(" " + i); }
Comments
Post a Comment