2D array help(stuck)

• 08-05-2012, 05:49 PM
Dex
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.
• 08-05-2012, 06:06 PM
awinston
Re: 2D array help(stuck)
Nonnullelements is not a 2D array. To define a 2D array of integers, do like so:

Code:

` int[][] Nonnullelements;`
For information on initializing a 2D array, check here: Initializing 2D Arrays
• 08-05-2012, 06:38 PM
Dex
Re: 2D array help(stuck)
Quote:

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

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.
• 08-05-2012, 06:43 PM
kjkrum
Re: 2D array help(stuck)
Post what you've tried.
• 08-05-2012, 06:48 PM
Dex
Re: 2D array help(stuck)
Quote:

Originally Posted by kjkrum
Post what you've tried.

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 }```
• 08-05-2012, 07:07 PM
Norm
Re: 2D array help(stuck)
Please post the full text of the error message.
• 08-05-2012, 07:14 PM
kjkrum
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.
• 08-07-2012, 03:52 AM
Junky
Re: 2D array help(stuck)
[nitpick]
Primitive arrays cannot have null elements.
• 08-07-2012, 01:33 PM
CustomException
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.
• 08-07-2012, 02:51 PM
Tolls
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.