I've pretty much finished this code but I have one small problem.
My task is to write a method named a2z, which accepts an array of strings as a parameter. This method searches array to find the element which should be the first element when you sort this array from a-to-z. After finding this element, this method should swap this element with the first element of the array.
this is my code:
public static void a2z(String [] a){
String min = a[0];
String temp = a[0];
for(int i = 0; i < a.length; i++){
if(a[i].compareTo(a[i+1]) <0 ){
min = a[i];
}else{
if(a[i].compareTo(a[i+1]) >0 ){
min = a[i+1];
}
}
min = a[0];
temp = a[/*index of min*/];
}
My question is how am I suppose to find the index of min, so that I can make temp equal that?
edit: i tried this
public static void a2z(String [] a){
String min = a[0];
String temp = a[0];
int indexOfMin = -1;
for(int i = 0; i < a.length; i++){
if(a[i].compareTo(a[i+1]) <0 ){
min = a[i];
indexOfMin = i;
}else{
if(a[i].compareTo(a[i+1]) >0 ){
min = a[i+1];
indexOfMin = i;
}
}
}
a[0] = min;
temp = a[i];
still didnt work
ArrayIndexOutOfBoundsExceptionwhenevera.length > 0.