0

I am trying to write a method in Java that receives an array and returns a new array where each number is printed that number of times. Here is an example input and output: "1 2 3 0 4 3" ---> "1 2 2 3 3 3 4 4 4 4 3 3 3". I am stuck and my program will not compile. Does anyone see where I am going wrong?

public static int [] multiplicity(int [] nums) {
    for (int i = 0 ; i < nums.length ; i++) {
        int size = nums.length + 1;
        int newNums[] = new int [size];
        for (int j = 0 ; j < nums.length ; j++) {
            int value = nums[j];
            for (int v = 0 ; v < value ; v++) {
                newNums[j + v] = value;
            }
        }
    }
    return newNums;
}
hpopiolkiewicz
  • 3,281
  • 4
  • 24
  • 36
Bill
  • 25
  • 2
  • 9
  • "_my program will not compile_" So tell us what the compiler error is. – takendarkk Oct 31 '14 at 02:33
  • It says "newNums can not be resolved to a variable" – Bill Oct 31 '14 at 02:34
  • 1
    Your program has a lot of flaws that I can see right now. Sit down with a piece of paper and go through this step by step. First and foremost, look at the way you are initializing your new int array. Does it make ANY sense for the example you gave us? – Alex K Oct 31 '14 at 02:34
  • Imagine what would happen if `nums` had a length of 0. Your first for loop would never be entered, `newNums` would never get created, your return statement tries to return something that doesn't exist. – takendarkk Oct 31 '14 at 02:35

3 Answers3

1

Your current code does not size your new array correctly, you could fix your compiler errors easily enough like

int size=nums.length+1;
int newNums [] = new int [size];
for (int i=0; i<nums.length; i++)
{
  // int size=nums.length+1;
  // int newNums [] = new int [size];

But that clearly won't allow you to populate all of your values. Instead (assuming you can't use a dynamic data-type like a Collection), you'll need to iterate the array once to get the final count of elements and then populate your array. Something like,

public static int[] multiplicity(int[] nums) {
    // first pass
    int count = 0;
    for (int num : nums) {
        for (int i = 0; i < num; i++) {
            count++;
        }
    }
    int[] ret = new int[count];
    count = 0;
    // second pass
    for (int num : nums) {
        for (int i = 0; i < num; i++) {
            ret[count++] = num;
        }
    }
    return ret;
}

Then you could test it like,

public static void main(String arg[]) {
    int[] in = { 1, 2, 3, 0, 4, 3 };
    int[] out = multiplicity(in);
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < out.length; i++) {
        if (i != 0) {
            sb.append(' ');
        }
        sb.append(out[i]);
    }
    String expected = "1 2 2 3 3 3 4 4 4 4 3 3 3";
    System.out.println(expected.equals(sb.toString()));
}

Output is

true
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • What does the colon in "for (int num : nums)" do? – Bill Oct 31 '14 at 02:53
  • @AlN. It is the [`for-each`](http://docs.oracle.com/javase/1.5.0/docs/guide/language/foreach.html) loop. You can read it as for each `num` in `nums`. Or you could use something like `for (int t = 0; t < nums.length; t++) { int num = nums[t]; /* ... */ }` instead. I prefer the `for-each`. – Elliott Frisch Oct 31 '14 at 02:55
0

Once you initialise your int[] newNums, you can't dynamically resize it. Initialising it again will discard the previous array.

Here's another way to solve the problem:

public static int [] multiplicity (int [ ] nums)
{
    // create a list to contain the output
    List<Integer> newNums = new ArrayList<Integer>();

    // for each incoming int
    if(nums != null) {
        for (final int i : nums)
        {
            // repeat adding the value
            for(int j = 0; j < i; j++) {
                newNums.add(i);
            }
        }
    }

    // now copy from the List<Integer> to the result int[]
    int[] result = new int[newNums.size()];
    for(int i=0; i < newNums.size(); i++) {
        result[i] = newNums.get(i);
    }

    // return the result
    return result;
}
Jason
  • 11,744
  • 3
  • 42
  • 46
0

You can't know the new array size until you explore the whole input array. So you can

  • Explore the whole array and compute the lengh, then, re-explore the input array and fill the new. You need only 1 memory allocation (only 1 new int[])
  • Create a vector and fill it. Then use the .toarray method

Exemple to fill the array (check he had the right size)

int k = 0
for(int i: nums) {
    for(int j = 0; j < i; j++) {
        newArray[k] = i;
        k++;
    }
}
Jason
  • 11,744
  • 3
  • 42
  • 46
crashxxl
  • 682
  • 4
  • 18