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 6 of 6

Thread: How do you set an object in array to null value?

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default How do you set an object in array to null value?

    Hi, I'm going to try to make my query as brief as possible because my code is very long. Please excuse me.

    I'm trying to replace (set) an object in my array to null. Lets say my array is "numbers" and "item" is the index of a particular object in the array.

    I tried:

    numbers.set(item, null);

    Is this correct? I get the error: "cannot find symbol - method set(int, <nulltype>)"

    I have never used "null" before, so any feedback is certainly welcome, but from what I know, null does nothing to change the size of the array -- it simply makes the value an empty hole.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How do you set an object in array to null value?

    Arrays are the exception in Java. instead of using a method, arrays are set directly, using square brackets to get both the indexed element and to set the data.

    // getting back an index value
    int[] numbers = new int[5];
    int[0] = 5; // setting
    System.out.println(int[0]); // getting

    Object arrays are the same as primitive arrays except that you can give them a null value.
    String[] list = new String[5];
    String[0] = null;

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    Arius (January 23rd, 2010)

  4. #3
    Junior Member
    Join Date
    Jan 2010
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How do you set an object in array to null value?

    Thank you for answering. I understand that way of setting, but isn't there a specific set method I can use for arrays (it is in the API I believe). Also, what are primitive arrays..? I thought arrays could only hold objects, so not ints, but only the Integer class, for example. Anyways, I will use your advice. Thanks again!

  5. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How do you set an object in array to null value?

    Arrays don't really have any methods defined, but to have the special method of getting/setting. Primitive arrays are simply arrays of primitives. They can hold any data type (primitive or object).

    I think what you're trying to reference to is an ArrayList. These can only hold object (because of generics implementation in Java, but that's a whole other can of worms). ArrayList does not have any of those square brackets stuff, and must be accessed in the way every other object can be.

    ArrayList<String> myStrings = new ArrayList<String>(5); // initial capacity of 5
    myStrings.set(0,null); // sets element 0 to null

  6. The Following User Says Thank You to helloworld922 For This Useful Post:

    Arius (January 24th, 2010)

  7. #5
    Junior Member
    Join Date
    Jan 2010
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How do you set an object in array to null value?

    Ah yes I know ArrayLists ahah I love them. All right, you cleared a lot of things up. Thanks for everything.

  8. #6
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: How do you set an object in array to null value?

    Always remember there is no such thing as a primitive array. An array is always an object. However an array can still take primitives.

    // Json

Similar Threads

  1. Post variable set to non-null vlaues
    By ss7 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 1st, 2009, 12:04 PM
  2. !=null
    By ss7 in forum Java Theory & Questions
    Replies: 11
    Last Post: October 31st, 2009, 02:48 PM
  3. Object array to int array?
    By rsala004 in forum Collections and Generics
    Replies: 1
    Last Post: October 30th, 2009, 04:09 AM
  4. Null Pointer Exception
    By MysticDeath in forum Exceptions
    Replies: 2
    Last Post: October 24th, 2009, 01:49 PM
  5. NullPointerException:null problem
    By derky in forum Exceptions
    Replies: 8
    Last Post: September 18th, 2009, 03:06 PM