So what I'm trying to do is pass the values of one class to the other. So with the code i have i believe what is happening is im not passing the values. But im passing the referenced location.

import java.util.Arrays;   
public class numbers {
    int[][] x = {{6,10,14,31,32},{12,23,12,11,22}};
    public void setX (int[][] x){
        this.x = x;
    public int[][] getX() {
        return this.x;

Theres the array with the values.

import java.util.Arrays;
public class Probability {
    public static void main(String args[]) {
        numbers aNumbers = new numbers();
        int[][] x = aNumbers.getX();
        for(int z=0; z<=x.length-1;z++)

Heres the class that id like to recive the array.

But this is my output -



Now theres more than likely a stupidly easy solution to this.

Recommended Answers

All 5 Replies

the System.out.print(ln) statements will, when received an object as parameter, call the toString method of that object.
the result you see is what you get when the object you try to print's type hasn't provided an implementation of toString, but uses that of the Object class.

just provide a toString method in your aNumbers class that prints the values the way you want them to be printed.

stultuiske's comments are correct, but his solution doesn't quite apply to your case. His advice is correct for printing an instance of a class you define, but not for printing ordinary arrays.

The Arrays class has a static toString method to convert ordinary arrays to sensible Strings for printing, like this:


there's also a deepToString method that works for multi-dimensional arrays, eg


ps Just for your info - the toString() method that everying inherits from Object tells you the type of the object and its unique hash code (normally same as its memory address). thus: [[I@1747b17 breaks down as
[[ - two dimensional array...
I - of integers...
@1747b17 - at that hex address in the JVM's heap.

James: considering the name of the class, it's about a 'list of numbers'. so why not implementing a toString in numbers?
what's wrong with him writing:

public String toString(){
  return Arrays.deepToString(this.x);

if his class remains having a single two dim. array as member, with variable instances that can have different values in those arrays, I would think this easier compared to repeating Arrays.deepToString(...) everywhere you want to print it.

What you suggest is what I would do too.
I was pitching my response a beginner level, hoping to avoid something like this, which would leave him in the same hole.

    public String toString(){
        return "numbers = " + this.x;

.. in other words, IMHO, the key learning point here was Array.deepToString rather than overriding toString.

Thank you guys so much! And it finally clicked now that i'm seeing it again im remembering the days in AP Comp. Sci. But thanks a million!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.