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());
   }
}
Chris Dargis
  • 5,891
  • 4
  • 39
  • 63
user547453
  • 1,035
  • 6
  • 22
  • 38
  • 1
    Similar: http://stackoverflow.com/questions/11399950/why-do-i-get-garbage-values-when-print-arrays-in-java/11399956#11399956 – jmj Jul 09 '12 at 18:09
  • possible duplicate of [Simplest way to print an array in Java](http://stackoverflow.com/questions/409784/simplest-way-to-print-an-array-in-java) – assylias Jul 09 '12 at 18:10
  • @JigarJoshi Way to point to your answer instead of what that post duplicated :) – Paul Bellora Jul 09 '12 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. – Sterling Jul 09 '12 at 18:19
  • @Jigar...your link did not answer my question. But I learnt something new. thanks. – user547453 Jul 09 '12 at 18:33

4 Answers4

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.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

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

get1()[0] will work.

Chris Dargis
  • 5,891
  • 4
  • 39
  • 63
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.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • 1
    @user547453 Glad I could help, but read also [answer that Jon Skeet gave](http://stackoverflow.com/a/11400660/1393766). It contains a loot of useful info. – Pshemo Jul 09 '12 at 18:16
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());

redDevil
  • 1,909
  • 17
  • 25