2

I have a sample of numbers. I am storing them in a List of data points with timestamps and values. Here is my code:

List<Number> samples = {55,67,57,67};
List<Number[]> dps = Lists.newArrayList();

Calender c1; c1.set(2013, 5, 20, 15, 44, 00);

int timeStartSec = (int) (c1.getTime().getTime() / 1000);

for(Number num: samples){
    dps.add(new Number[] { timeStartSec, num});
    timeStartSec = timeStartSec+5;
}

System.out.println("Final DataPoints " + dps);

Output:

Final DataPoints [[Ljava.lang.Number;@af8358, [Ljava.lang.Number;@d80be3, [Ljava.lang.Number;@1f4689e, [Ljava.lang.Number;@1006d75, [Ljava.lang.Number;@1125127, [Ljava.lang.Number;@18dfef8, [Ljava.lang.Number;@15e83f9, [Ljava.lang.Number;@2a5330, [Ljava.lang.Number;@bb7465,]

Why is it printing as java.lang.Number instead of the appropriate value?

arshajii
  • 127,459
  • 24
  • 238
  • 287
user2325703
  • 1,161
  • 1
  • 8
  • 10

2 Answers2

9

Arrays don't override the toString method (annoyingly). Have a look at Arrays.toString.

Number[] nums = {1, 2, 3, 4};
System.out.println(nums);
System.out.println(Arrays.toString(nums));
[Ljava.lang.Number;@14b7453
[1, 2, 3, 4]

In your case, you will want to call Arrays.toString on each element of dps, printing / storing the results.

arshajii
  • 127,459
  • 24
  • 238
  • 287
1

instance of Array does not have the toString() method. You may want to use ArrayList instead:

List<Number> samples - {55,67,57,67};
List<List<Number>> dps = new ArrayList<List<Number>>();

for(Number num: samples){

    dps.add(Arrays.asList(timeStartSec, 10));
    timeStartSec = timeStartSec+5;
}
System.out.println("Final DataPoints " + dps);

EDIT: or the static method defined on the class Arrays: Arrays.toString()

Jiri Kremser
  • 12,471
  • 7
  • 45
  • 72