?
Solved

Can array copying be done with for each?

Posted on 2009-04-28
10
Medium Priority
?
148 Views
Last Modified: 2013-11-23
Just checking.
Is there any syntax that allows me to for example copy one array from another with for each?
//The "normal" way
for(int i = 0; i < length; i++)
  array[i] = otherArray[i];
 
//I'd prefer for:each. Does it have to be like this?
int i = 0;
foreach(float element:Array)
  otherArray[i++] = element;

Open in new window

0
Comment
Question by:letharion
  • 4
  • 3
  • 3
10 Comments
 
LVL 6

Accepted Solution

by:
__geof__ earned 1000 total points
ID: 24248580
It should work alright but I am not sure about the index(i++) Ithink you may get an empty one in otherArray[0]. I would split it in 2 lines:


int i = 0;
foreach(float element:Array){
  otherArray[i] = element;
  i++
}

Open in new window

0
 
LVL 92

Assisted Solution

by:objects
objects earned 1000 total points
ID: 24248588
you can it would look like this:

int i = 0;
foreach(float element : otherArray)
  array[i++] = element;

or alternately

http://helpdesk.objects.com.au/java/how-do-i-resize-a-java-array
0
 
LVL 92

Expert Comment

by:objects
ID: 24248594
should be:

int i = 0;
for (float element : otherArray)
  array[i++] = element;

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 6

Expert Comment

by:__geof__
ID: 24248623
I ran a quick test and it works alright with i++ as index. code attached. I waould also note that you could use otherArray = array.clone();
float[] array = {1,2,3,4,5,6};
float[] otherArray = new float[array.length];
int i = 0;
for(float f:array){
	otherArray[i++] = f;
}

Open in new window

0
 
LVL 6

Author Comment

by:letharion
ID: 24248787
array.clone() was a good idea, but I'm not really specifically looking to just copy arrays.
I see my question was unclear.

My emphasis should have been on "Does it have to be like this?"
Adding the index seperately looks pretty ugly, but I assume it's the only way to accomplish the task.
If I don't ask if there's another way I won't find out though ;)

If the index is indeed needed, then I prefer the "old" style :)

>>I would split it in 2 lines:
I don't think I understood why that was?
0
 
LVL 92

Expert Comment

by:objects
ID: 24248824
there are methods available to copy arrays, see the link I posted above
otherwise I would suggest the first way as you need to know the index


0
 
LVL 6

Author Comment

by:letharion
ID: 24248894
Since both of you are telling me the same thing, I take that as sufficient proof that it's true.
Points?
0
 
LVL 92

Expert Comment

by:objects
ID: 24248909
splitting them is fine with me

0
 
LVL 6

Author Comment

by:letharion
ID: 24248920
Sounds good, geof will just have to shout a bit if that's not ok.

I appreciate the help from both of you :)
0
 
LVL 6

Expert Comment

by:__geof__
ID: 24248929
Glad I could help ;)
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

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…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month15 days, 18 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