0

I'm a novice in Java programming. I couldn't catch in the following case why I can't print an array by this line:

System.out.print(arr());

Instead of all of results I got this: '[D@60e1e567'

What did I do wrong?

public class test {

    public static void main(String[] args) {
        System.out.print(arr());
    }

    public static double[] arr() {
        double res;

        int count=0;
        double[] anArray = new double[100000000];


        int k=0;
        for (int j=2;j<101;j++){
            for(int i=2; i<101;i++) {
                res=Math.pow(j,i);
                anArray[k]=res;
                k++;
            }
        }

        return anArray;         
    }
}
Fabian Schmengler
  • 24,155
  • 9
  • 79
  • 111
Leo
  • 649
  • 3
  • 9
  • 20
  • 3
    because you can't just print an array with `System.out.println()`. – kaysush Feb 15 '13 at 13:04
  • 2
    Please note that java has its coding style. Your class should begin with a capital letter. – qben Feb 15 '13 at 13:29
  • 1
    @qben , There is no rule in java that your class name should starts with capital, It may be small or capital – Scarecrow Feb 15 '13 at 13:32
  • 2
    @Swarnendu see java naming conventions http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-135099.html#367. Naming conventions are not rules but conventions, of course. – qben Feb 15 '13 at 13:43
  • 3
    Thats why it is not necessary to name your class with a name starts with a Capital letter .... user user1813163 is a novice so plz don't confuse him... – Scarecrow Feb 15 '13 at 13:49
  • I learnt this right after "Hello world" so I think a novice also should at least know of this. – qben Feb 15 '13 at 14:37

6 Answers6

1

You cant print arrays like this

System.out.print(arr());

Use this:

System.out.print(Arrays.toString(arr()));

In order to do this you should also add

import java.util.Arrays;

in the beginning of the file

Also your array is to big. 100000000 doubles will probably not fit in array. You probably want to change the Data structure, or limit the size. Otherwise you risk getting a java.lang.OutOfMemoryError: Java heap space

user000001
  • 32,226
  • 12
  • 81
  • 108
  • user000001 hm, the compiler says: Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.util.Arrays.copyOf(Unknown Source) – Leo Feb 15 '13 at 13:11
  • Do you actually need so many elements? Why not use an `ArrayList` instead? It changes its size dynamically... – user000001 Feb 15 '13 at 13:12
  • yes, I would use distinct way in order to get a list of all of possible numbers – Leo Feb 15 '13 at 13:15
  • Not sure if your combinations fit in main memory. If they do, you can increase the heap space. A tutorial for that is **[here](http://blogs.opcodesolutions.com/roller/java/entry/solve_java_lang_outofmemoryerror_java)**. If they don't, then you will probably have to store your data in a file. But I think this is out of the scope of this question. Maybe you should ask a new one, if these methods don't work for you – user000001 Feb 15 '13 at 13:22
1
System.out.println(Arrays.toString(arr()));

Returns a string representation of the contents of the specified array. The string representation consists of a list of the array's elements, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (a comma followed by a space). Elements are converted to strings as by String.valueOf(double). Returns "null" if array is null.

Achintya Jha
  • 12,735
  • 2
  • 27
  • 39
  • hm, the compiler says: Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.util.Arrays.copyOf(Unknown Source) – Leo Feb 15 '13 at 13:09
  • @user1813163 That is not because you are trying to print array. That errror is because of heap space. – Achintya Jha Feb 15 '13 at 13:17
1

You can iterate through the array to see it's values, or call the Collections convenience method which roughly does the same thing: Arrays.toString(arr())

public static void main(String[] args) {
    double[] d = arr();
    for(double x : d) {
        System.out.println(x);
    }
}
mprivat
  • 21,582
  • 4
  • 54
  • 64
1

you can't print arrays directly. you need somthing like:

double[] array=arr();

for(double x: array)
   System.out.print(x+" ");
Arpit
  • 12,767
  • 3
  • 27
  • 40
0

You did nothing wrong. Arrays don't implement a nice toString() by default. The best way to print a human-readable output is to use Arrays.asList(arr()).

n0rm1e
  • 3,796
  • 2
  • 28
  • 37
0

Instead of printing whole array you should print each elements there:

double[] resultAry =arr();
for(int i=0; i< resultAry.length; i++ ){
System.out.println(resultAry[i]);
}
bhupesh
  • 25
  • 6