So I had a task to combine two arrays and now I am having trouble with converting the if-statement inside the combine method into a ternary operator. I am not well-experienced with that method. Could someone show me how to do that?
static int[] combine( int[] sorted1, int[] sorted2) {
int[] result = new int[sorted1.length + sorted2.length];
if (isSorted(sorted1) && isSorted(sorted2)) {
int c = 0;
for (int i = 0; i < sorted1.length; i++) {
result[i] = sorted1[i];
}
for (int j = sorted1.length; j < sorted1.length + sorted2.length; j++) {
result[j] = sorted2[c];
c++;
}
int tausch;
for (int i = 0; i < result.length; i++) {
for (int j = 1; j < result.length - i; j++) {
if (result[j - 1] > result[j]) {
tausch = result[j - 1];
result[j - 1] = result[j];
result[j] = tausch;
}
}
}
return result;
} else {
int[] resultleer = new int[0];
System.out.print("Arrays not sorted. Cancelled.");
return resultleer;
}
}
public static boolean isSorted(int[] array){
for (int i = 0; i < array.length-1; i++) {
if(array[i] <= array[i+1]){
}else{
return false;
}
}
return true;
}
Aucun commentaire:
Enregistrer un commentaire