user2924808
user2924808

Reputation: 1

How to properly alter the value of an array element in Java?

This probably counts as very easy stuff, but since I'm only about 5 weeks into my Comp Sci course, please excuse any ignorance on my part.

As a part of our grade this year, we have to implement a 'Spread-and-Die' game. The interface for this game is a 12 by 12 ASCII layout in the command line.

The code I have made to work out the problem, not my actual code due to a ridiculous length, I have here:

class PrintTest
{
    public static void main (String[] args)
    {
        final char[] array = {'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'};


        char[] array1 = array;
        char[] array2 = array;
        char[] array3 = array;

        array2[6] = 'P';

        System.out.println("\n");

        System.out.print (array1[0]);
        System.out.print (array1[1]);
        System.out.print (array1[2]);
        System.out.print (array1[3]);
        System.out.print (array1[4]);
        System.out.print (array1[5]);
        System.out.print (array1[6]);
        System.out.print (array1[7]);
        System.out.print (array1[8]);
        System.out.print (array1[9]);
        System.out.print (array1[10]);
        System.out.print (array1[11]);

        System.out.print("\n");

        System.out.print (array2[0]);
        System.out.print (array2[1]);
        System.out.print (array2[2]);
        System.out.print (array2[3]);
        System.out.print (array2[4]);
        System.out.print (array2[5]);
        System.out.print (array2[6]);
        System.out.print (array2[7]);
        System.out.print (array2[8]);
        System.out.print (array2[9]);
        System.out.print (array2[10]);
        System.out.print (array2[11]);

        System.out.print("\n");

        System.out.print (array3[0]);
        System.out.print (array3[1]);
        System.out.print (array3[2]);
        System.out.print (array3[3]);
        System.out.print (array3[4]);
        System.out.print (array3[5]);
        System.out.print (array3[6]);
        System.out.print (array3[7]);
        System.out.print (array3[8]);
        System.out.print (array3[9]);
        System.out.print (array3[10]);
        System.out.print (array3[11]);

        System.out.print("\n");
    }
}   

I also, as you can evidently see, have not been able to find out how to print whole arrays, so any hints there would also be useful, given the ugliness of my solution.

Anyway, when that code is complied/run, I get this:

######P#####
######P#####
######P#####

Given my admittedly-limited understanding of Java and programming in general, only the second line should have a 'P' in it. For some reason, the statement changing element 6 of array2 is also doing the same for the other two arrays.

Help would be muchly appreciated.

Upvotes: 0

Views: 284

Answers (3)

Alexis C.
Alexis C.

Reputation: 93892

Because primitive arrays are like objects, they hold a reference. So when assigning array2[6] = 'P', it's like saying array[6] = 'P' and hence array1[6] = 'P' and array3[6] = 'P'.

To avoid that, you can call the method clone(), while creating your arrays.

char[] array1 = array.clone();
char[] array2 = array.clone();
char[] array3 = array.clone();

If you want to copy specify elements for you array, you can use Arrays.copyOfRange :

char[] array1 = Arrays.copyOfRange(array, 0, array.length);
                                     ^    ^       ^
                                     |    |       |

                                     |    |    indice to

                                     | indice from

                                     |
                            array to be copied

Also to print your arrays, you can use a for loop.

for(int i = 0; i < array1.length; i++)
     System.out.print(array1[i]);

System.out.println();

Upvotes: 2

upog
upog

Reputation: 5526

Since all your array are pointing to same reference you are getting same values printed

you can create a function to print array, to avoid redundant code

class PrintTest
{
    public static void main (String[] args)
    {
        final char[] array = {'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'};
        char[] array1 = array;
        char[] array2 = array;
        char[] array3 = array;
        array2[6] = 'P';
        printArr(array1);
        printArr(array2);
        printArr(array3);

    }

    public static void printArr(char[] array){
        for(int i=0;i<array.length;i++){
            System.out.print(array[i]);         
        }
        System.out.println();
    }
}   

Instead of assigning array reference , try creating new array as suggested by other answers

Upvotes: 1

Robin Green
Robin Green

Reputation: 33103

This is because you've only created one array, and referred to that exact same array from 3 variables.

You need to create 3 separate arrays, like this:

char[] array1 = {'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'};
char[] array2 = {'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'};
char[] array3 = {'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'};

Upvotes: 0

Related Questions