tags:

views:

357

answers:

7

I'm trying to print the values of an int array to a local file. However, I can't seem to find a way to print the integers out in their standard form (1,2,3) instead of a heap address: ([I@1befab0)

My code fragment is below:

        PrintWriter pr = new PrintWriter("file");

        for (int i=0; i<views.length ; i++){
            pr.println(Arrays.toString(views));
        }
        pr.close();
    }
    catch (Exception e)
    {
        e.printStackTrace();
        System.out.println("No such file exists.");
    }
}

What could I be doing wrong?

A: 

The toString() method on an array gives the "[I@1befab0" String.
So you need to loop over the array.

You can split the problem in two:

  1. for building the String (you can check the value in debug)

    StringBuilder b = new StringBuilder();
    for (Object o : views) {
      b.append(String.valueOf(o));
    }
    
  2. for writing the String to a file
    ...

KLE
+3  A: 

You're trying to print the whole array out each time. Try this:

(I've made minimal changes. The fact that you're catching Exception is a generally bad idea though, and you should usually close the writer in a finally block.)

try
{
    PrintWriter pr = new PrintWriter("file");    

    for (int i=0; i<views.length ; i++)
    {
        pr.println(views[i]);
    }
    pr.close();
}
catch (Exception e)
{
    e.printStackTrace();
    System.out.println("No such file exists.");
}
Jon Skeet
Closing the PrintWriter in every iteration seems like a mistake?
waxwing
Many thanks for your help!
MajorMajor
@waxwing: Doh! Fixing...
Jon Skeet
A: 

When you print out the array, it calls the array's toString() function. So, what you are seeing is the reference value of the "views" array for as many values exist in the array.

As Jon pointed out, you actually need to print the values of the array. Of course, this assumes that the array contains objects that are nicely formatted via toString (or that the array holds primitives).

JasCav
A: 

Are you writing to a file to persist the array contents? If so, serialize the object (array in this case). There's a link on how to deserialize the object so you can retrieve the value on the linked page.

OMG Ponies
+1  A: 

is the array perhaps a multiple dimension array? If so, use java.util.Arrays.deepToString.

PrintWriter pr = new PrintWriter("file");

for (int i=0; i<views.length ; i++){
  pr.println(Arrays.deepToString(views));
}
pr.close();
Matt
A: 

Most answers here focus on the fact that you need to iterate over the array instead of printing all of it in one go. But Arrays.toString(...) is supposed to return the same as Arrays.asList(...).toString(), which, incidentally, should be in the [1,2,3] form.

I suggest therefore, as Matt does in his answer, that views in fact a double-nested array.

The fixed code would in that case be:

try {
    PrintWriter pr = new PrintWriter("file");

        for (int i=0; i<views.length ; i++){
           pr.println(Arrays.toString(views[i]));
        }
        pr.close();
    }
    catch (Exception e)
    {
        e.printStackTrace();
        System.out.println("No such file exists.");
    }
}
waxwing
A: 
PrintWriter pr = new PrintWriter("file");        
for (int i=0; i<views.length ; i++){            
pr.println("" + views[i]);        
}        
pr.close();

It doesn't get much easier than that.

gshauger