4

So I'm trying to repeat an int[] array by the values that are in it. So basically if you have an array {1,2,3,4} your output will be

{1,2,2,3,3,3,4,4,4,4}

or if you get

{0,1,2,3}

your output is

{1,2,2,3,3,3}.

I know for sure there has to be two for loops in here but I can't seem to figure out the code to make it copy the value in the array. I can't get 2 to out 2,2, Any help will be much appreciated, thank you.

edit here the code I thought would work

public static int[] repeat(int []in){
    int[] newarray = new int[100];

    for(int i = 0; i<=in.length-1;i++){

        for(int k= in[i]-1;k<=in[i];k++){

            newarray[i] = in[i];   

        }
    }
    return newarray;
}

I thought that this would work but it just returns the same list, or sometime if I change it around ill just get 4 in the new array.

user1832483
  • 107
  • 2
  • 8
  • 6
    Please show what you have tried so far. And please keep in mind that stackoverflow is not a code factory... – home Nov 17 '12 at 20:17
  • You have to figure out 2 things: the size of your new array, and how you're going to place the old values into the new array. The first problem can be solved via a simple loop, while the second can be solved via a nested loop, like you mentioned. – jma127 Nov 17 '12 at 20:19
  • What are you using this for anyway? Maybe there is a better solution to the problem you are trying to solve. – Sietse Nov 17 '12 at 20:41

3 Answers3

1

Try:

LinkedList<Integer> resList = new LinkedList<Integer>();
for(int i = 0 ; i < myArray.length ;  ++i) {
    int myInt = myArray[i];
    for(int j = 0 ; j < myInt ; ++j) { // insert it myInt-times
        resList.add(myInt);
    }
}
// TODO: build the result as an array : convert the List into an array
Jean Logeart
  • 52,687
  • 11
  • 83
  • 118
1

This will dynamically build a new array of the correct size and then populate it.

    int[] base = { 1, 2, 3, 4 };
    int size = 0;
    for( int count : base ){
        size += count;
    }

    int[] product = new int[size];

    int index = 0;
    for( int value : base ){
        for(int i = 0; i < value; i++){
            product[index] = value;
            index++;
        }
    }

    for( int value : product ){
        System.out.println(mine);
    }
thedan
  • 1,230
  • 2
  • 9
  • 13
  • This solution works great but the problem is that I don't understand how exactly it works, I havent been studying Java for very long, the for loop with ("int count : base") what exactly is that doing – user1832483 Nov 17 '12 at 20:39
  • Basically it loops through the array one item at a time placing the current item in the variable I declared on the left. so for(int value : base) loops through base setting "value" to each item one after another. Google "Java for each loop" for a better explanation :P – thedan Nov 17 '12 at 20:41
  • check out this question: http://stackoverflow.com/questions/85190/how-does-the-java-for-each-loop-work – thedan Nov 17 '12 at 20:50
0

Try this:

int[] anArray = { 
    0, 1, 2
};
int[] newArray = new int[100];
int cnt=0;
for(int i=0; i<anArray.length; i++)
{
    for(j=1;j>0;j--)
    {
        newArray[cnt]=anArray[i];
        cnt++;
    }
}
matsev
  • 32,104
  • 16
  • 121
  • 156
Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110