0

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

3
  • Besides the logical problem this code will throw an ArrayIndexOutOfBoundsException whenever a.length > 0. Commented Jan 23, 2016 at 23:04
  • 2
    You'd better rethink about that code first. You shouldn't compare a[i] with a[i+1], but a[i] with min. Commented Jan 23, 2016 at 23:04
  • @2handz , If one of the answers works for you, it's good (not mandatory) to accept it ( stackoverflow.com/help/accepted-answer & meta.stackexchange.com/a/5235/270025 ). This also applies for your past/future posts. Commented Jan 23, 2016 at 23:53

2 Answers 2

1

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?

Sign up to request clarification or add additional context in comments.

2 Comments

I'm not understanding, i did this and it gives me i cannot be resovled to a variable because it is under the for loop.
@2handz , do this after you have a correct program that finds your min. Your actual solution is not good.
0

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 )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.