1
public class Arrys {
    private int[] nums;

    //Step 3
    public Arrys (int arrySize) {
        nums = new int[arrySize];
    }

    public int [] getNums (){
        return nums;
    }
}

Test class:

public class TestArrys
{
    public static void main(String args[])
    {
        //Step 4
        Arrys arry = new Arrys(10);
        System.out.println("\nStep4 ");
        for(int index = 0; index < arry.getNums().length; index++) {
            System.out.print(arry.getNums());
        }
    }
}

It's incredibly simple, that is why I think I'm doing something fundamentally wrong. All I want is to display the value of the array.

This is what I get back. I am totally lost, there is nothing in my book that explains this nor does googling it help.

Step4 
[I@1ac88440[I@1ac88440[I@1ac88440[I@1ac88440[I@1ac88440[I@1ac88440[I@1ac88440[I@1ac88440[I@1ac88440[I@1ac88440[I@1ac88440

3 Answers 3

8

You're trying to print the array itself out several times. This code:

for(int index = 0; index < arry.getNums().length; index++) {
    System.out.print(arry.getNums());
}

should (potentially) be this:

for(int index = 0; index < arry.getNums().length; index++) {
    // println instead of print to get one value per line
    // Note the [index] bit to get a single value
    System.out.println(arry.getNums()[index]);
}

Or rather more simply:

for (int value : arry.getNums()) {
   System.out.println(value);
}

When you call toString() on an array, it returns something like [I@1ac88440 where the [ indicates that it's an array, I indicates the array element type is int, and @xxxxxxxx is the address in memory. It's diagnostic, but not really helpful in most cases.

Use Arrays.toString to get a more useful representation.

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

3 Comments

You are so right. I glanced at the question, saw the [l@1ac88440, and thought I knew what the question was about -- but I was wrong.
Maybe I need to take up coffee.
Dang. I feel like such an Idiot right now! Thank you SO much!!
2

Try

System.out.println(java.util.Arrays.toString(arry.getNums()));

instead of the loop.

By default, printing out an array will not give you a very useful string. To get the kind of output you're hoping for, you could loop through the array and print out each element yourself... or you could let java.util.Arrays do the dirty work.

Comments

0

It returns array:

public int [] getNums ()

This loop prints array reference getNums().length times...

for(int index = 0; index < arry.getNums().length; index++) {
    System.out.print(arry.getNums());
}

Try this:

int [] nums = arry.getNums();
for(int index = 0; index < nums.length; index++) {
    System.out.print(arry.nums[index]);
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.