tags:

views:

156

answers:

4
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
+2  A: 

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.

Michael Myers
A: 

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]);
}
Jarek
+8  A: 

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.

Jon Skeet
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.
Michael Myers
@mmyers: You weren't actually wrong at all, as far as I can see...
Jon Skeet
@Jon Skeet: Maybe I need to take up coffee.
Michael Myers
Dang. I feel like such an Idiot right now! Thank you SO much!!
Phil
A: 

what dose get nums means is this context ?

amam