?
Solved

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

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

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


   


0
Comment
Question by:NyTR0
  • 2
3 Comments
 
LVL 16

Expert Comment

by:imladris
ID: 24189335
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)
    eoarray[i]=array[i].ThirdElementOfMyStruct;

0
 

Author Comment

by:NyTR0
ID: 24222953
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

0
 
LVL 16

Accepted Solution

by:
imladris earned 2000 total points
ID: 24226436
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)
    eoarray[i]=array[i].ThirdElementOfMyStruct;

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

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses
Course of the Month15 days, 15 hours left to enroll

850 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