?
Solved

Replicating an array in java

Posted on 2012-04-09
6
Medium Priority
?
304 Views
Last Modified: 2012-04-11
I would like to know of the most efficient way of doing the following. Please note, I know how this can be achieved by putting this in a for loop. However, if there is a more efficient way of doing it, I would like your recommendation.

Suppose I have an array of Strings:

String[] names = new String[] {"Alpha", "Beta", "Gamma"};

I would like to create a new array with the Strings in the original array replicated (repeated) multiple times.

Example:

String[] replicatedNames = new String[30];

{"Alpha", "Beta", "Gamma","Alpha", "Beta", "Gamma","Alpha", "Beta", "Gamma","Alpha", "Beta", "Gamma","Alpha", "Beta", "Gamma","Alpha", "Beta", "Gamma","Alpha", "Beta", "Gamma","Alpha", "Beta", "Gamma","Alpha", "Beta", "Gamma","Alpha", "Beta", "Gamma"};

I was hoping there would be a straightforward way of doing this with System.arraycopy(), but that does not seem to be the case.
0
Comment
Question by:ctoan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 3

Expert Comment

by:gnemi
ID: 37825984
String[] replicate = ArrayUtils.addAll(names, names);
0
 
LVL 1

Author Comment

by:ctoan
ID: 37826203
That would give me an array of 6 Strings. Not the solution that I am looking for.
0
 
LVL 11

Expert Comment

by:anilallewar
ID: 37826847
One of the things that you could try is
String[] names = new String[] {"Alpha", "Beta", "Gamma"};

ArrayList<String> array = new ArrayList<String>();

//Repeat # of times required
for (i=0;i<10;i++){
         Collections.addAll(array , name);
}

String [] newNames = new String[array.size];
array.toArray(newNames);

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Assisted Solution

by:gnemi
gnemi earned 1000 total points
ID: 37829796
That would give me an array of 6 Strings. Not the solution that I am looking for.
You would need to repeatedly call it to do more. That is the only option I'm aware of without using a loop.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 1000 total points
ID: 37830217
I was hoping there would be a straightforward way of doing this with System.arraycopy(), but that does not seem to be the case.

That is the most efficient way of doing it, rather than creating a Collection and then turning it into an array, but of course you do need a loop
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 37832113
:)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month13 days, 2 hours left to enroll

777 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