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 7072387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:48:10+00:00 2026-05-28T05:48:10+00:00

I am a newbie to Java. I have an array of objects that have

  • 0

I am a newbie to Java. I have an array of objects that have a string field. I can concatenate all of the strings into an string array by looping, but it’s very inelegant.

int numObj = obj.length;
String[] strArray = new String[numObj];
for (int i = 0; i < numObj; i++) {
    strArray[i] = obj[i].strField;
}

Is there a way to concatenate that single field from all of the objects into a string array in one command? e.g.:

String[] strArray = (String[]){obj[].strField};

This doesn’t work because obj[] is an array and so it doesn’t have any fields, but using {obj.strField} doesn’t work either, because there is no object called obj. BTW I really don’t have to recast the field or do .toString() because it is already a string.

I looked at many, many of the other posts (but perhaps not enough?) related to this but I still could not figure this out. There are some that refer to converting an object array to a string array, but I don’t think those posts mean converting a particular field in the objects, but the object itself, as an uncast type.

In MATLAB this would be trivial: strCellArray = {obj.strField}; would create a cell array of strings from all of the strFields in obj instantly.

Thanks for your help.

  • 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-05-28T05:48:10+00:00Added an answer on May 28, 2026 at 5:48 am

    What you did is the only way. You don’t have to create a variable for the length of the array, though. And using public fields is, 99.99% of the times, a very bad idea:

    String[] strings = new String[objects.length];
    for (int i = 0; i < objects.length; i++) {
        strings[i] = objects[i].getStringField();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Java Newbie here. I have a JFrame that I added to my netbeans project,
I am a newbie to Java Web Application development. So far all I have
Newbie question. I have a NSMutableArray that holds multiple objects (objects that stores Bezier
I am newbie in java but I think I have done well teaching myself
I have an array in $_POST: Array ( [tags] => Javascript,PHP,Java,C++,Python) How can i
I think that this is a newbie type question but I have quite understood
I'm a newbie on Java EE and got a problem that I don't understand
Newbie here...can I write one program which incorporates .NET LINQ and also various Java
Newbie Java question - On all the posts for Auto Scaling the Text view,
(Newbie to Java, old time C# guy.) I have noticed a lot of the

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.