Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 871621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:39:59+00:00 2026-05-15T10:39:59+00:00

First, it is long post so if you need clarification please let me know.

  • 0

First, it is long post so if you need clarification please let me know.

I’m new to Java and having difficulty deciding whether I should use int[] or Integer[].
I wrote a function that find odd_number from int[] array.

public int[] find_odd(int[] arr) {
        int[] result = new int[arr.length];
        for(int i=0; i<arr.length; i++) {
            if(arr[i] % 2 != 0) {
                //System.out.println(arr[i]);
                result[i] = arr[i];
            }
        }
        return result;
    }

Then, when I pass the int[] array consisting of some integer like below:

int[] myArray = {-1, 0, 1, 2, 3};
int[] result = find_odd(myArray);

The array “result” contains:

0, -1, 0, 1, 0, 3

Because in Java you have to define the size of array first, and empty int[] array element is 0 not null. So when I want to test the find_odd() function and expect the array to have odd numbers (which it does) only, it throws the error because the array also includes 0s representing “empty cell” as shown above.

My test code:

public void testFindOddPassValidIntArray() {
        int[] arr = {-2, -1, 0, 1, 3};
        int[] result = findOddObj.find_odd(arr);

        //check if return array only contain odd number
        for(int i=0; i<result.length; i++) {
            if(result[i] != null) {
                assert is_odd(result[i]) : result[i];
            }

        }
    }

So, my question is should I use int[] array and add check for 0 element in my test like:

if(result[i] != 0) {
  assert is_odd(result[i] : result[i]
}

But in this case, if find_odd() function is broken and returning 0 by miscalculation, I can’t catch it because my test code would only assume that 0 is empty cell.

OR should I use Integer[] array whose default is null? How do people deal with this kind of situation?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T10:40:00+00:00Added an answer on May 15, 2026 at 10:40 am

    My advice is generally you shouldn’t use arrays. You should use Lists instead. At that point, the choice is made for you. You have to use Integer not int because Java generics don’t support primitive types (unlike, say, C#).

    public List<Integer> findOdd(List<Integer> in) {
      List<Integer> ret = new ArrayList<Integer>();
      for (Integer i : in) {
        if (i % 2 == 1) {
          ret.add(i);
        }
      }
      return ret;
    }
    

    Since you’re new to Java, this may introduce a few new concepts to you.

    Java Collections don’t have the same limitations as arrays. For example, you don’t have to declare the size of an ArrayList when you create it. Array allocation is abstracted away by this particular class.

    Now all this assumes you don’t want the result array to be the same size as the input array. There are valid reasons for this (eg when doing vector/matrix maths). The two ways of handling this kind of thing are to use Integer (so you can put null for empty values but this then requires you to test for null) or by using a sentinel value (eg Integer.MAX_VALUE).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.