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);

    }
}
2
  • maintain a counter, add the element and increment the counter Commented Jun 15, 2015 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. Commented Jun 15, 2015 at 16:01

2 Answers 2

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);
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I was trying to use only arrays. Thanks for the help.
Arrays waste space in this case, Lists are dynamic and can grow in size.
@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 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.
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;

1 Comment

Thanks for helping me. It's worked!.I was trying to use only arrays

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.