Package Interview;import Java.util.arrays;import Java.util.collections;import java.util.comparator;import java.util.list;/** * * @author * calculation in the array there are several pairs of opposite numbers * */public class Listsort {public static void main (string[] args) {Listsort Test = new Listsort (); list<integer> list = Arrays.aslist (9,1,3,8,-10,7,-9,-2,-8,2); context<integer> contextmin= New Context<> (Test.new mincomparatorlist ());//context<integer> cont extmax= New Context<> (Test.new maxcomparatorlist ()); list<integer> listmin = contextmin.sortlist (list);//List<integer> Listmax = contextmax.sortlist (list); integer[] Arrays = (integer[]) Listmin.toarray (); System.out.println (GetCount (Arrays,getmidindex (arrays));} public static int GetCount (integer[] arrays,int mid) {int count=0;for (int. i=0;i<mid;i++) {for (int j=mid;j< arrays.length;j++) {if (arrays[i]+arrays[j]==0) {count++;}}} return count;} public static int Getmidindex (InteGer[] arrays) {int start = 0; int end =arrays.length-1; int mid = 0; while (start<=end) {mid = (End-start)/2+start; if ((mid>0 && arrays[mid]>0&& arrays[mid-1]<0)) {return mid; }else if (mid>0 &&arrays[mid]>0 && arrays[mid-1]>0) {end = Mid-1; }else if (arrays[mid]<0 && arrays[mid+1]<0) {start=mid+1; }} return mid;} /* * Policy mode */class Mincomparatorlist implements comparator<integer>{@Overridepublic int compare (integer O1, integer O2) {return o1>o2?1:-1;}} Class Maxcomparatorlist implements comparator<integer>{@Overridepublic int compare (integer o1, integer o2) { return o1<o2?1:-1;}} public static class Context<t>{private comparator<t> comparator;public Context (comparator<t> Comparator) {this.comparator = comparator;} Public list<t> sortlist (list<t> List) {collections.sort (LISt,comparator); return list;}}}
There are several pairs of opposite numbers in the computed array