1

get1() method returns an array. I do get the output but the value from get1() is [D@addbf1

My question is there any way to directly use the returned array to get the values in inf1[0] and inf1[1] and show it in the output statement?

I am aware of other ways of showing the output. But I want to know whether I can directly retrieve elements of the returned array.

class vehicle{
   double [] inf1 = new double[2];
   void set(double d, double s) {
     inf1[0]=d;
     inf1[1]=s;
   }
   double[] get1() {
     return inf1;
   }
}

public class calc2 {
   public static void main(String args[]) {
     vehicle ob = new vehicle();
     ob.set(56.24, 75);
     System.out.println("The time taken to cover "+ob.get1());
   }
}
5
  • 1
    Similar: stackoverflow.com/questions/11399950/… Commented Jul 9, 2012 at 18:09
  • possible duplicate of Simplest way to print an array in Java Commented Jul 9, 2012 at 18:10
  • @JigarJoshi Way to point to your answer instead of what that post duplicated :) Commented Jul 9, 2012 at 18:10
  • You get the output of [D@addbf1 because you're printing the reference. So its printing the reference of the array returned by the method instead of the elements like you expected. Commented Jul 9, 2012 at 18:19
  • @Jigar...your link did not answer my question. But I learnt something new. thanks. Commented Jul 9, 2012 at 18:33

4 Answers 4

6

My question is there any way to directly use the returned array to get the values in inf1[0] and inf1[1] and show it in the output statement?

Sure:

double[] result = obj.get1();
System.out.println("Result: " + result[0] + "," + result[1]);

Or:

System.out.println("Result: " + Arrays.toString(obj.get1());

The [D@addbf1 is just the result of calling toString() on an array. If you want to get at the values within an array, you normally just access each element individually using an array index expression: array[index]

(It's not clear whether your question is really about accessing the array values, or converting an array into text for display purposes.)

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

1 Comment

my question was for accessing. Thanks and your suggestion also worked for me. I appreciate it.
1

Yes, you can reference the elements of the array via the get1() method:

get1()[0] will work.

Comments

1

You can use a collection class like ArrayList.

Declare ArrayList<double> inf1 = new ArrayList<double>();

In get1()

inf1.add(d);
inf1.add(s);

and in calc2, System.out.println("The time taken to cover "+ob.get1());

Comments

1

try

System.out.println("The time taken to cover "+ob.get1()[0]);

ob.get1() will return your array so you can now use [index] on it.

1 Comment

@user547453 Glad I could help, but read also answer that Jon Skeet gave. It contains a loot of useful info.

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.