Using array names as a variable in loops (Java)

I encountered a problem today when I was trying to simplify my code by using array names as a variable in loops, but the compiler keeps telling me that the variable(array name) is a char, not an array, so how do I fix this to implement my question title? p.s I know there are still many things to fix with the code below, but I just want to present an example about the following of using array names as a variable in loops:

(compare[i+1])[i] > (compare[i])[i]

class ArraySort{

    public static void main(String[] args){

    int[] a = {4, 5, 3};
    int[] b = {7, 5};
    int[] c = {7, 8, 9};
    int[] d = {4, 9, 9};
    int[] e = {5, 1};
    int[] f = {3, 8, 2, 5};     

    System.out.println("Before sort: 453 75 789 499 51 3825");
    System.out.println("After sort:");

    char[] compare = {'a', 'b', 'c', 'd', 'e', 'f'};
    char temp;

    for(int i = 0; i < 3; i++ ){ // 3 --> max number amount to compare;  
        for(int j = 0; j < compare.length-1; j++){
            if((compare[i+1])[i] > (compare[i])[i]){ // problem is here
                temp = compare[i];
                compare[i] = compare[i+1];
                compare[i+1] = temp;    
            }
        }
    }

}

Upvotes: 1

Views: 1520

Answers (2)

Korashen
Korashen

Reputation: 2191

Your code did not compile in my Java8 environment, I had to change some things.

First, the array initialisation needed to be:

int[] a = {4, 5, 3};
int[] b = {7, 5};
int[] c = {7, 8, 9};
int[] d = {4, 9, 9};
int[] e = {5, 1};
int[] f = {3, 8, 2, 5};

Then the init of the compare and temp object have been changed to arrays:

int[][] compare = {a, b, c, d, e, f};
int[] temp;

The complete method now looks like this:

class ArraySort
{

    public static void main(String[] args)
    {

        int[] a = {4, 5, 3};
        int[] b = {7, 5};
        int[] c = {7, 8, 9};
        int[] d = {4, 9, 9};
        int[] e = {5, 1};
        int[] f = {3, 8, 2, 5};

        System.out.println("Before sort: 453 75 789 499 51 3825");
        System.out.println("After sort:");

        int[][] compare = {a, b, c, d, e, f};
        int[] temp;

        for (int i = 0; i < 3; i++)
        { // 3 --> max number amount to compare;
            for (int j = 0; j < compare.length - 1; j++)
            {
                if ((compare[i + 1])[i] > (compare[i])[i])
                { // problem is here
                    temp = compare[i];
                    compare[i] = compare[i + 1];
                    compare[i + 1] = temp;
                }
            }
        }

    }
}

This compiles without any errors or warnings. Can't say, if the result is as expected, as your expected result is not 100% clear to me. But the mentioned compiler issues are gone.

Using chars or strings, containing variable names, it not really working in Java. There are ways, of course, you can use reflections to achieve this. But I strongly recommend not to use reflections in this case.

=== EDIT1 ===

I am using int[][] compare = {a, b, c, d, e, f}; instead of char[] compare = {'a', 'b', 'c', 'd', 'e', 'f'};. Why?

You have defined some arrays at the beginning of your code a = {4, 5, 3} etc. So you have an array with the variable name of a. If you use 'a' (mind the ') in a char array, it is nothing else but the character 'a', it does not have any reference to the actual array, that is stored in the variable a. You could also go for 'x' or any other character.

Using the variable name in a char and not the variable itself requires you to create the "link" between the char 'a' and the variable a manually. You can use reflections for that. More about reflections later.

By not using a char array but an int[][] array for compare, you create an array, that will hold int[] "things". And your initial arrays a = {4, 5, 3} etc. are exactly that format int[]. This enables you to just use the variable itself in your compare array. So in the line int[][] compare = {a, b, c, d, e, f};, the character a is not a char, but the actual variable, referencing to your initially defined array.

Having it a bit shortend:

int[] a = {4, 5, 3};
int[] b = {7, 5};
int[][] compare = {a, b};

is the same as

int[][] compare = {{4, 5, 3}, {7, 5}};

If you want to go with reflections ... you should go and read up about it. It's a complex topic. Very very very simply said, just to get you the mind set: With reflections, you can access (and manipulate) the source code during runtime.

Imagine it like this

int[] a = {1, 2, 3};
char access = 'a';

int[] reflectionOfA = (int[])myClass.getMember(access);

!Not Real Java Code! The myClass.getMember(String) method receives a String value, plain text. Then it will crawl through the myClass and search for a member with the name of the value ofaccess, which is 'a'. It will find the int[] array a and return that. As the getMember Method can not know, what type the searched member is (is it int[], String, whatever else?), you need to provide that information. In this case it's a casting. But I think the actual Java Reflections take another parameter, which defines the return type.

If you know nothing about reflections, do not use them yet! Read about them. They have a big downside as they are not very performant (they crawl through the objects on every call, no caching, no optimization, whatsoever).

Upvotes: 4

MikeFHay
MikeFHay

Reputation: 8983

You cannot easily access local variables by using the string of their name in Java. Instead you can just reference the variable directly.

int[][] compare = {a, b, c, d, e, f};

Upvotes: 0

Related Questions