简体   繁体   中英

java basic array error

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

Keep track of the index used along the way, updating it whenever min is updated.

For example:

int indexOfMin = -1;

// later...
min = a[i];
indexOfMin = i;

Make sense?

Try this:

public static void main(String[] args) {
    // just a trick to avoid iterating and printing (do not use it if the argument is null)
    System.out.println(Arrays.asList(a2z(new String[]{"x","c","b","d"})));
}

// I've also changed the method type (to avoid printing in it)
public static String[] a2z(String[] a) {
    // be cautious - java.lang.ArrayIndexOutOfBoundsException is ugly
    if ( a == null || a.length == 0 ) {
        return a;
    }
    // consider that the first element is the "minimum"
    String min = a[0];
    int minIndex = 0;
    // start with 1 in for, because the first element was already considered
    for (int i = 1; i < a.length; i++) {
        if (min.compareTo(a[i]) > 0 ) {
            // update the minimum in every step and update its position
            min = a[i];
            minIndex = i;
        }
    }
    // change the first element with the "minimum" element
    String temp = a[0];
    a[0] = a[minIndex];
    a[minIndex] = temp;
    return a;
}

Output :

[b, c, x, d]

Obs :

Because of the standard codes , A is before a , so the following line:

System.out.println(Arrays.asList(a2z(new String[]{"X","c","b","d"})));

will print

[X, c, b, d]

because X is the first element in the alphabetical order (so, the swap will be made between X and X ).

If you want to get the following output:

[b, c, X, d]

you need to use compareToIgnoreCase in the comparison:

if (min.compareToIgnoreCase(a[i]) > 0 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM