java - Delete consecutives values in ArrayList of Int -
i have created arraylist
30 000 values, after been sorted, have lot of consecutive values, example:
22001 22002 22003 22004 22010 22011 22020
and want have : 22001 22004 22010 22011 22020
arraylist<long> l = new arraylist<long>(); // values collections.sort(l); arraylist<long> liste = new arraylist<long>();
i didn't test it, should work (for lists containing 3 or more elements). it's unoptimized regarding memory usage.
arraylist<long> resultlist= new arraylist<long>(); long prevnumber= l.get(0); (int = 1; < l.size() - 1; i++) { if (prevnumber.equals(l.get(i) - 1) && l.get(i + 1).equals(l.get(i) + 1)) { //print 'inbetween' consecutive number } else { //not consecutive, copy result list resultlist.add(l.get(i)); } prevnumber= l.get(i); }
Comments
Post a Comment