Solved

Replicating an array in java

Posted on 2012-04-09
6
295 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
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
Technology Partners: 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 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
xampp tool 12 58
servlet example 17 58
What's wrong with this code? 4 34
junit example issue 2 22
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

685 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