convert or copy an array of struct to an array (or something I can use)

Posted on 2009-04-20
Last Modified: 2013-12-29
I have this
 Mystruct array[] = new array[100];
System.out.print (array[i].ThirdElementOfMyStruct); // it works

but I  would copy array[i].ThirdElements into an array
What can I do ?


Question by:NyTR0
    LVL 16

    Expert Comment

    It sounds like you have a structure (Mystruct), which contains a bunch of fields. One of the fields is named ThirdElementOfMyStruct. Your description doesn't specify what the type of the field ThirdElementOfMyStruct is. So I'll assume that it is some kind of object (not a primitive like int) for the moment.

    If that is correct, then there is no way of doing this in a single operation. It would have to be done much like the printing. Set up an array, and then copy the stuff into it:

    ThirdElementObj eoarray[]=new ThirdElementObj[100];
    for(i=0; i<100; ++i)


    Author Comment

    Unfortunaltely the type of the field is not an object but int,
    another question:  array[i].ThirdElementOfMyStruct can return many values of ThirdElementOfMyStruct  (example:  1332 and 955)  and I would see if I can find  the value 67343 for example in                           array[i].ThirdElementOfMyStruct
    Is it possible?
    System.out.println("Serial number"+ dda[i].snr);
    // it prints 
    Serial number 23432
    Serial number 67343
    Serial number 43556
    // I would know if 67343 is contained in dda[i].snr

    Open in new window

    LVL 16

    Accepted Solution

    Again, no apparent way of doing it in a single operation. To get it into an array, would then be something like:

    int eoarray[]=new int[100];
    for(i=0; i<100; ++i)

    To find a serial number you would check each element:

    int i;
    for(i=0; i<dda.length; ++i)
    {   if(dda[i].snr==67343)break;
    if(i<dda.length) // serial number found


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
    This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now