Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8844939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:36:49+00:00 2026-06-14T11:36:49+00:00

String[] strarray = new String[dataLine.size()]; strarray = dataLine.toArray(strarray); PrintWriter pr = new PrintWriter(test.txt); for

  • 0
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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T11:36:52+00:00Added an answer on June 14, 2026 at 11:36 am

    I think you are looking for

    pr.println(strarray[i]);
    

    in your for-loop.

    Unrelated to your question you should change this

    String[] strarray = new String[dataLine.size()];
    strarray = dataLine.toArray(strarray);
    

    to

    String[] strarray = dataLine.toArray(new String[0]);
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two arrays: int[] intArray = new int[]{point1,point2,point3,point4}; String[] strArray = new String[]{worda,wordb,wordc,wordd};
string[] strArray = new string[10] { 21.65, 30.90, 20.42, 10.00, 14.87, 72.19, 36.00, 45.11,
I've got : string[] strArray = new string[3] { 1, 2, 12 }; And
Since .NET arrays are covariant, the following works in C#: var strArray = new
String number = {'number':000645} net.sf.json.groovy.JsonSlurper slurper = new net.sf.json.groovy.JsonSlurper() def resource = slurper.parseText(number) System.out.println(resource.number)
What I'd prefer is something like: string[] strArray = {Hi, how, are, you}; string
An array is defined of assumed elements like I have array like String[] strArray
//string NSString *haystack = @.test {test:test} .test2{dasf:asdF}; //pattern NSString *needle = @[^{}]*{[^{}]*}; What I
string abc = 07:00 - 19:00 x = int.Parse(only first two characters) // should
I have a string: strArray= -------9---------------; I want to find 9 from the string.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.