Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By kjkrum

Thread: 2D array help(stuck)

  1. #1
    Dex
    Dex is offline Member
    Join Date
    Mar 2012
    Posts
    29
    Rep Power
    0

    Default 2D array help(stuck)

    Hey,

    I'm trying to set the number of none null elements in each row, using an array.
    E.g Nonnullelements[] = {4,5,7,5,4}

    My rows need to have null as well as non null elements.
    I've been trying to do this using a nested for loop , but so far to no avail.

    Thanks.

  2. #2
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: 2D array help(stuck)

    Nonnullelements is not a 2D array. To define a 2D array of integers, do like so:

    Java Code:
     int[][] Nonnullelements;
    For information on initializing a 2D array, check here: Initializing 2D Arrays
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  3. #3
    Dex
    Dex is offline Member
    Join Date
    Mar 2012
    Posts
    29
    Rep Power
    0

    Default Re: 2D array help(stuck)

    Quote Originally Posted by awinston View Post
    Nonnullelements is not a 2D array. To define a 2D array of integers, do like so:

    Java Code:
     int[][] Nonnullelements;
    For information on initializing a 2D array, check here: Initializing 2D Arrays
    sorry I dont think i explained it properly.

    I already have a 2D array, and I am trying to use a 1D array (nonnullelements) to set the number of non null elements in each row of my 2D array. Hope that makes more sense.
    Last edited by Dex; 08-05-2012 at 07:41 PM.

  4. #4
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: 2D array help(stuck)

    Post what you've tried.
    Get in the habit of using standard Java naming conventions!

  5. #5
    Dex
    Dex is offline Member
    Join Date
    Mar 2012
    Posts
    29
    Rep Power
    0

    Default Re: 2D array help(stuck)

    Quote Originally Posted by kjkrum View Post
    Post what you've tried.
    Java Code:
    for(int row = 0; row < numRows; row++){
      Array2D[row].length = nonNullElements[row] // Using array "nonNullElements" to set the number of non null objects in each row of my Array2D
    }

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,894
    Rep Power
    25

    Default Re: 2D array help(stuck)

    Please post the full text of the error message.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: 2D array help(stuck)

    First of all, the length of an array does not tell you how many elements are non-null. Second, you can't set the length of an array; it's final. Third, you haven't explained where nonNullElements[] comes from. I thought your question was how to create it, but now you have it and you're trying to use it for something else?

    If you want help, don't post little snippets of code that tell us nothing. The best thing to do is post a SSCCE.
    Norm likes this.
    Get in the habit of using standard Java naming conventions!

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: 2D array help(stuck)

    [nitpick]
    Primitive arrays cannot have null elements.

  9. #9
    Join Date
    May 2012
    Posts
    18
    Rep Power
    0

    Default Re: 2D array help(stuck)

    [nitpick]
    I assume you mean an array that holds primitive types cannot hold null elements. The array itself is an object.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: 2D array help(stuck)

    Not so sure, as an array (by its very nature) cannot be a primitive, then Junky's statement holds true, since he clearly means "an array of primitives".
    People write about String arrays all the time, for example, and it's clear that they don't think the array is a String.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. I'm new and stuck, please help :(
    By XoVoX in forum Advanced Java
    Replies: 6
    Last Post: 07-15-2012, 08:48 PM
  2. Stuck with a little aspect of the array list
    By kingkongjaffa in forum New To Java
    Replies: 2
    Last Post: 09-18-2011, 05:07 PM
  3. Really stuck need help
    By B_Tank88 in forum New To Java
    Replies: 45
    Last Post: 07-18-2011, 08:52 PM
  4. really stuck now..
    By shongo in forum Advanced Java
    Replies: 2
    Last Post: 11-09-2008, 03:56 AM
  5. Stuck in need of help!
    By Zombie_Leg! in forum New To Java
    Replies: 1
    Last Post: 09-23-2008, 03:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •