Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Find first null in an array of objects

  1. #1
    Member
    Join Date
    Apr 2013
    Posts
    43
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Find first null in an array of objects

    Hi!

    I'm a java beginner and canīt figure out whatīs wrong with my code.
    The question is: How do I find the first null in an array of objects, arr[10], and then add an object, obj, to that index.

    I can think of something like this:

    for(int i = 0; i<10; i++)
    {
            if(arr[i] == null)
            {
                arr[i] = obj;
                return i; //return statement because we are inside a int-method
            }
    }

    I can also think of something like "if (arr[i] != null) then do nothing, else do arr[i] = obj", but I think thatīs an ugly solution.

    Thankful for all kind of inputs

    Hank


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    589
    My Mood
    Sleepy
    Thanks
    10
    Thanked 87 Times in 80 Posts

    Default Re: Find first null in an array of objects

    Have you tried your solution? Does it work?

  3. #3
    Member
    Join Date
    Apr 2013
    Posts
    43
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Find first null in an array of objects

    No, Iīm at work. Do you se any direct problem with my solution. The "==" for example.

    My teacher wrote:

    // is it a free spot at index i? if(arr[i] != null) {
    arr[i] = obj;

    and I don't understand it. I thought that "if(arr[i] != null)" meant that if arr[i] is not null...

  4. #4
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    589
    My Mood
    Sleepy
    Thanks
    10
    Thanked 87 Times in 80 Posts

    Default Re: Find first null in an array of objects

    Correct! != is not equals.

  5. #5
    Member
    Join Date
    Apr 2012
    Posts
    161
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Find first null in an array of objects

    It looks fine. Although instead of a return statement, you could use break to break out of the loop and then return.

  6. #6
    Junior Member
    Join Date
    Oct 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Find first null in an array of objects

    You can try:
    for(int i = 0; i<10; i++)
    {
            try{
                    if(arr[i].length==0);        //if null, will throw exception
            }catch(Exception e){
                    arr[i] = obj;
                    break;
            }
    }

    It's ugly but it should work fine.

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,301
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Find first null in an array of objects

    Quote Originally Posted by iKiWiXz View Post
    You can try:
    ...
    It's ugly but it should work fine.
    I would not recommend this approach for the following reasons a) Exceptions are meant to handle errors during runtime, not for logic b) it assumes the objects within the array have a numeric length property - if they do not then this will not compile c) other exceptions are possible within try clause

Similar Threads

  1. Passed array value is null
    By NoobException in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 23rd, 2013, 10:01 PM
  2. Array with null (same for Arraylist)
    By Alex L in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 4th, 2011, 11:51 PM
  3. Array list returns null
    By Scotty in forum Collections and Generics
    Replies: 3
    Last Post: April 14th, 2011, 06:50 AM
  4. FAQ: find variable in an array of objects
    By dalek in forum Object Oriented Programming
    Replies: 1
    Last Post: April 5th, 2010, 12:40 PM
  5. How do you set an object in array to null value?
    By Arius in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 25th, 2010, 02:50 AM