1. Member
Join Date
Mar 2012
Posts
29
Rep Power
0

## 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. Student
Join Date
Jul 2012
Location
United States
Posts
328
Rep Power
9

## 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

3. Member
Join Date
Mar 2012
Posts
29
Rep Power
0

## Re: 2D array help(stuck)

Originally Posted by awinston
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 06:41 PM.

4. ## Re: 2D array help(stuck)

Post what you've tried.

5. Member
Join Date
Mar 2012
Posts
29
Rep Power
0

## Re: 2D array help(stuck)

Originally Posted by kjkrum
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. ## Re: 2D array help(stuck)

Please post the full text of the error message.

7. ## 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.

8. ## Re: 2D array help(stuck)

[nitpick]
Primitive arrays cannot have null elements.

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

## 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. Moderator
Join Date
Apr 2009
Posts
13,541
Rep Power
27

## 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.

#### Posting Permissions

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