String[] strarray = new String[dataLine.size()];
strarray = dataLine.toArray(strarray);
PrintWriter pr = new PrintWriter("test.txt");
for (int i=0; i<strarray.length; i++)
{
pr.println(Arrays.toString(strarray));
}
pr.close();
when I look at test.txt for this, it outputs a set of data I do not want (specifically only a few of the last ones) in comparison to calling
System.out.println(Arrays.toString(strarray));
any idea why this is happening and how to fix this?
Thanks!
EDIT:
let me give a more concrete example.
System.out.println outputs
[1040, 0, 6, 1, 11/12/2012, 3, 0, 0, 1, 12/27/2011, 0, 0]
[1041, 0, 6, 1, 11/13/2012, 3, 0, 0, 1, 12/28/2011, 0, 0]
while pr.println(Arrays.toString(strarray)) prints
[1041, 0, 6, 1, 11/13/2012, 3, 0, 0, 1, 12/28/2011, 0, 0]
[1041, 0, 6, 1, 11/13/2012, 3, 0, 0, 1, 12/28/2011, 0, 0]
and pr.println(strarray[i]); prints
1041
0
6
1
11/13/2012
3
0
0
1
12/28/2011
0
0
the desired output I want on my output file is the first one. However, the last two do not provide me with this.
Thanks!
I think you are looking for
in your for-loop.
Unrelated to your question you should change this
to
because your way of doing it will first create an array of the desired size, and then change the reference to another array, wich will make your first array eligible for garbage collection. So don’t create it, if you don’t need it.