3

I'm trying to identify multiples of 3 from an input array and insert those multiples in to output array.Using a count variable I can declare the output array. But then how can I insert multiples in to output array?.

public class MultipleOfThreeInAnArray {

static int count;
static int[] result;


public static void choseMultiplesOfThree(int[] input) {

    for (int i = 0; i < input.length; i++) {

        if (input[i] % 3 == 0) {
            count++;

            // insert into output array?
            int[] result = new int[count];

        }
    }
public static void main(String[] args) {
       int[] input = { 3, 2, 5, 6, 9, 30 };
       choseMultiplesOfThree(input);

    }
}
Kaveen
  • 129
  • 1
  • 2
  • 11
  • maintain a counter, add the element and increment the counter – Uma Kanth Jun 15 '15 at 15:56
  • Basically, you have the right code, just set `count` to zero outside your loop, and then initialize `result`. Also put `count++;` *after* your insertion. – River Jun 15 '15 at 16:01

2 Answers2

1
public class MultipleOfThreeInAnArray {
    public static void choseMultiplesOfThree(int[] input) {
        int[] output = new int[input.length];
        int index = 0;
        for (int i = 0; i < input.length; i++) {
            if (input[i] % 3 == 0) {
                output[index++] = input[i];
            }
        }
        System.out.println(Arrays.toString(output));
    }

    public static void main(String[] args) {
        int[] input = {3, 2, 5, 6, 9, 30};
        choseMultiplesOfThree(input);
    }
}

I suggest you to use a List rather than an array.

public class MultipleOfThreeInAnArray {
    public static void choseMultiplesOfThree(int[] input) {
        List<Integer> output = new ArrayList<Integer>();
        for (int i = 0; i < input.length; i++) {
            if (input[i] % 3 == 0) {
                output.add(input[i]);
            }
        }
        System.out.println(output);
    }

    public static void main(String[] args) {
        int[] input = {3, 2, 5, 6, 9, 30};
        choseMultiplesOfThree(input);
    }
}
Tom
  • 16,842
  • 17
  • 45
  • 54
Uma Kanth
  • 5,659
  • 2
  • 20
  • 41
  • I was trying to use only arrays. Thanks for the help. – Kaveen Jun 15 '15 at 16:31
  • Arrays waste space in this case, Lists are dynamic and can grow in size. – Uma Kanth Jun 15 '15 at 16:32
  • @UmaKanth Please mind that I did a small "clean up" for your code. Both examples had unused variables, which I removed, I added a generic information to the list, because you shouldn't use [raw types](http://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it) and I changed the way you print the list, because your version had a missing parenthesis and it is not necessary to convert the list to an array to use `Arrays.toString` on it. – Tom Jun 15 '15 at 19:53
1

You can Simply use a list where you will insert those elements then finally just convert this list to an array using .toArray method:

public class MultipleOfThreeInAnArray {
    static Integer[] result;
    List<Integer> list = new ArrayList<Integer>();


    public static void choseMultiplesOfThree(int[] input) {
        for (int i = 0; i < input.length; i++) {
            if (input[i] % 3 == 0) {
                // insert into output array
                list.add(Integer.valueOf(input[i]));
            }
        }
    result = list.toArray(new Integer[list.size()]);
    }

    public static void main(String[] args) {
           Integer[] input = { 3, 2, 5, 6, 9, 30 };
           choseMultiplesOfThree(input);
        }
    }

That should do the trick, and note that you have to use Integer with List.

EDIT:

If you want to use an array of int, you can :

1- Use ArrayUtils from apache commons:

int[] result = ArrayUtils.toPrimitive(list.toArray(new int[list.size()]));

2- Loop throught the list elements and put them in an int array:

    int[] result = new int[list.size()];
    int i = 0;
    for (Integer e : list) {
        result[i] = e.intValue();
        i++;
    }
    return result;
Community
  • 1
  • 1
cнŝdk
  • 31,391
  • 7
  • 56
  • 78