Solved

Replicating an array in java

Posted on 2012-04-09
6
298 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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 
LVL 3

Assisted Solution

by:gnemi
gnemi earned 250 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 250 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

729 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