java - how to count number of array elements that lie between two elements of given array -
java - how to count number of array elements that lie between two elements of given array -
i want write method when supplied array of ints following. each pair of array elements combine them , set them list of inner class objects. compare each element in array , check if fit between each pair values. (i.e. have array 0, 2, 4 create illustration pair (0,4) , check value 2 indeed between 0 , 4 counter increase). tried next code returned 0. how prepare or there easier way accomplish that? care homecoming value correct. give thanks you
import java.util.*; import java.util.map; import java.lang.*; public class prac1 { public int count(int[] a){ int k = 0; class ptemp{ int first = -1; int sec = -1; public ptemp(int first, int second){ int f = first; int s = second; } } list<ptemp> r = new arraylist<ptemp>(); (int = 0; < a.length; i++) { (int j = i+1; j < a.length; j++) { r.add(new ptemp(a[i], a[j])); r.add(new ptemp(a[j], a[i])); //system.out.println("["+a[i] +","+a[j]+"]"); //system.out.println("["+a[j] +","+a[i]+"]"); } } iterator<ptemp> ir = r.iterator(); while (ir.hasnext()){ ptemp p = ir.next(); (int = 0; < a.length; i++){ if (((p.first < a[i]) && (a[i] < p.second)) || ((p.first > a[i]) && (a[i] > p.second))){ k = k + 1; } } } homecoming k; } public static void main(string[] args){ int[] = {0, 2, 4}; prac1 pr = new prac1(); system.out.println(pr.count(a)); } }
found sec bug in add-on 1 mentioned alex d:
class ptemp{ int first = -1; int sec = -1; public ptemp(int first, int second){ int f = first; int s = second; } }
should be:
class ptemp{ int first = -1; int sec = -1; public ptemp(int first, int second){ this.first = first; this.second = second; } }
now fields set.
java list iterator
Comments
Post a Comment