byte[]array=new byte[10];
for(byte i=0;i<10;i++){
array[i]=(byte)((i*2)+(2*Math.random()));
System.out.println(array[i]);
}
boolean even=false;
byte next=0,range;
List<Byte>ranges=new ArrayList<Byte>();
for(byte i=0;i<10;i++){
if(i<next){
i=next;
}
else{
if(array[i]%2==0)even=true;
else even=false;
if(even==true){
next=(byte)(i+1);
while(next<10){
if(array[next]%2==0)break;
next++;
}
if(next==10)next=(byte)(i+1);
}
if(even==false){
next=(byte)(i+1);
while(next<10){
if(array[next]%2==1)break;
next++;
}
if(next==10)next=(byte)(i+1);
}
}
ranges.add((byte)i);
}
for(byte i=0;i<ranges.size();i++){
if(i==0)System.out.print(array[ranges.get(0)]);
if(i>0){
if(ranges.get(i)-ranges.get(i-1)>1)System.out.print("-"+array[ranges.get(i)]);
else System.out.print(", "+array[ranges.get(i)]);
}
}
System.out.println();
You need to import java.util.*; The code can go straight into main method. I was too lazy to make a sort method so I just made a series of random numbers that are definitely bigger than the previous one and put it into an array. Everytime it's different. Feel free to ask any questions. I did not add documentation, but to generalize:
-The first loop gets the array data
-The second loop makes a list of all beginnings and ends of odds and evens in the range form that you specified. I don't know how to explain that, but I'm 99% sure this is what you need.
-The last loop just outputs the values in the way that you wanted.
As a side note, I used byte because it's smaller than int, that's why you see (byte) everywhere if you didn't know. If you change it to int you may not have to do that.