Imagine an array defined as follows:
int n = 10;
int[][] arr = new int[0][n];
Let's say this array is passed into several methods without preserving n. Can we obtain n from this array?
arr[0].length certainly won't work.
Can we obtain n from this array?
No.
The way I think about it is this. The line
int[][] arr = new int[a][b];
is essentially the same as
int[][] arr = new int[a][];
for (int i = 0; i < a; i++)
arr[i] = new int[b];
If a == 0, the for loop does nothing, so the value of b is irrelevant.
since every second level in this array is distinct, you will not be able to find out.
consider this:
int[][] a = new int[10][20];
a[0] = new int[5];
a[1] = new int[8];
for(int[] x : a) {
System.err.println(x.length);
}
and you will see that each X component is actually a pointer to another int[] object that can be set and mondified independently. by doing
int[][] a = new int[0][n];
you never actually create any of those pointers, and you don't create any of the Y components in there.
Related
Here is an exercise:
Write an algorithm that declares and fills an array of 7 values numerics, bij putting the values to 0.
In Python I have to do this:
note = []
for i in range(7):
note.append(0)
print( note )
I have tried below in Java... I am obliged to use an arrayList?
I would like the do with an array empty.
int[] notes;
for(int i=0;i<7;i++){
notes.add(0);
}
try this:
int[] notes = new int[7];
for(int i=0;i<7;i++){
notes[i] = 0;
}
When declaring your notes array you must initialize it and specify the length of it. If not it will throw a compilation error. Proceed as follows:
int[] notes = new int[7]; //Declare the size of the array as 7
for(int i=0;i<7;i++){
notes[i] = 0; //Iterate the positions of the array via the variable i.
}
If you want to use an ArrayList:
List<Integer> notes = new ArrayList<Integer>(); //You don't have to define the length.
for(int i = 0; i < 7, i++) {
notes.add(0);
}
You can also do like this.
Arrays.fill(notes, -1);// But notes has to be declared.
or
int[] notes = {0,0,0};
For the special case of int and a desired value of 0, you don't even have to write out the loop assignment. You just need this:
int[] notes = new int[7];
This will create an array of ints, each with the Java default value of 0.
You need to use an assignment operation.
notes[i] = 0;
Another option - using streams:
int[] notes = IntStream.range(0, 7).map(i -> 0).toArray();
But if you need exactly 0, you can simply do it like:
int[] notes = new int[7]; // but it for 0 only
I'm trying to multiply an array and a 2d array on java and my program compiles but keeps returning the error java.lang.NullPointerException; null when I try to input anything into it. Here is my code so far:
static double[][] productWithDiagonal(double[] a, double[][] b)
{
double[][] c = new double[3][];
{
for (int i = 0; i < b.length; ++i) {
for (int j = 0; j < b[1].length; ++j) {
c[i][j] = a[j] * b[i][j];
}
}
}
return c;
}
Thanks
This here:
double[][] c = new double[3][];
Only instantiates your "rows". You need something like
double[][] c = new double[3][3];
Or more useful probably
... c = new double[b.length][b[0].length];
instead. But just to be sure: those numbers there matter; you should make sure that b for example is really a "regular rectangle" shaped matrix - so that all rows have the same number of columns. And of course a should have the same number of columns as b, too. You could add such checks in the beginning of your method; to ensure that the shapes of a and b actually allow for this multiplication!
You see, in Java, a two-dim array is nothing but an array that contains another array. Your initial code would only initiate that "outer" array, leaving the "inner" arrays at null.
Hi i'd like to populate array in Java. I'm trying to do it on easily way
int[] tab = null;
for(int i=0; i<5; i++)
{
tab[i]=i;
}
Why this constructions not working ?
error: null pointer exception
This is how you implement it
int[] tab = new int[5];
for(int i=0; i<tab.length; i++)
{
tab[i]=i;
}
Dynamic allocation means you should be using one of the Collection instances, such as a List. If you have some constraint (such as homework rules) that says you must use an array, then you have to do the following:
declare and instantiate the array to some arbitrary size: int[] intArray = new int[20];
during the addition of the elements, check if the index is equal to the length - 1: if(index == length - 1) , and if true,
create a temporary array of a size that is some multiple of the original array's size (usually 2x) int[] temp = new int[intArray.length*2];
copy the elements from the original array to the temp array
reassign the temp variable value to the original: intArray = temp;
after the loop, create a new array that is the correct length and copy all values from the original to the new one. (this is because the size will most certainly be wrong -- the length will be more than the number of elements).
In pseudo code, it looks like this:
int[] intArray = new int[20];
int index = 0;
loop:
if(index >= intArray.length - 1){
int[] temp = new int[intArray.length * 2];
copyAll: intArray->temp // use System.arrayCopy
intArray = temp;
}
intArray[index] = value;
index++;
endloop:
// resize because intArray will likely be too big
int[] temp = new int[index];
copyAll intArray -> temp;
intArray = temp;
As you can see, it's much, much nicer to use the Collection framework:
List<Integer> intList = new ArrayList<Integer>();
loop:
intList.add(value);
endloop
Here is how I would implement this array in the simplest way possible:
int[] tab = new int[]{0, 1, 2, 3, 4};
Your code doesn't work because you never create a new object to hold the elements of the array, it just creates space for a reference to that object. Before you can reference the object using
tab[i]
you must create a new object like this:
tab = new int[5];
So, if you really want to implement this variable in a for loop for whatever reason, here's how you should do it:
final int ARRAY_SIZE = 5;
int[] tab = new int[ARRAY_SIZE];
for(int i=0; i<ARRAY_SIZE; i++){
tab[i] = i;
}
Now, if you want to increase the number of elements in your array, you just need to change the ARRAY_SIZE variable.
You need to initialize your array before you use it:
int[] tab = new int`[5];
If you need something that is dynamicaly allocated use something like Vector or ArrayList:
For example:
ArrayList<Integer> list = new ArrayList<Integer>();
int elements_to_add = 5;
for(int i=0; i<elements_to_add; i++)
{
list.add(new Integer(i) );
}
Despite the fact that you declare this array and pass it null, you don't create any object here. You have only a reference to null. That's the reason of your error. What you have to do is initialize this array like int []myArray = new int[5];
Something like this should work :
int[] tab = new int[5];
for(int i=0; i<5; i++){
tab[i] = i;
}
Can someone please explain to me why this does not work:
int[][] array = new int[0][5];
int n = array[0].length;
while this does work:
int[][] array = new int[5][5];
int n = array[0].length;
I was doing some unit test to find the width and height of a NxN array when I ran into this problem.
You are making a zero-sized array that will hold an array of size 5
int[][] array = new int[0][5];
You must at least give dimension of 1
int[][] array = new int[1][5];
which becomes basically a One-dimensional array
int[] array = new int[5];
int[][] a = new int[x][y] is a shorthand for a loop that creates x arrays of size y:
int[][] a = new int[x][];
for (int i = 0; i < x; i++)
a[i] = new int[y];
When x is zero then y is irrelevant since no array of size y is ever created. In fact in the general case there's no reason why the x arrays need to be the same size, or even exist, as any of the elements of a could just be null rather than an int array.
int[][] a = new int[0][5];
you are trying to make zero sized array, remember in a 2D array first [] represents the size of the array.
Example:
int[][] a = new int[2][4];
a = {(1,2,3,4}, {5,6,7,8}}
System.out.println(a.length);// would return `2`
i want to create an array of 5 indexes, which will have an index with another whole array in each one. each array within the 5 index will need to be as many indexes as follows (10, 100,1000,10000) but i dont know how to fill an array like this inside of my for loop ,that is typically used with an array, without it running into infinity, because where i have
for(int x = 0; x < array.length; x++),
i cant use the x variable in here;
int[x<--(syntax error)] array = {ten = new int[arraySize], hundred = new int[arraySize], thousand = new int[arraySize], tenthousand = new int[arraySize],
without it telling me there is a syntax error . i don't know what to do.
all this code is part of a method of its own class as well if that helps understand better.
public int ArrayArray(int arraySize, int randomNumber) {
arraySizes = arraySize;
for(int x = 0; x < array.length; x++) {
size = 0;
Random gen = new Random(randomNumber);
int[]ten;
ten = new int[arraySize];
int[] hundred;
hundred = new int[arraySize];
int[]thousand;
thousand = new int[arraySize];
int[]tenThousand;
tenThousand = new int[arraySize];
int[] array = {ten[x] = gen.nextInt(10), hundred[x] = gen.nextInt(100),
thousand[x] = gen.nextInt(1000), tenThousand[x] = gen.nextInt(10000)};
return array[];
}
this changes my question a little i think ive got it after having worked on it. does this look like it will return what i want it to do? im going to have a driver that i will call this method with a given array size and a given number of random integers.
If you are trying to return and array your function try this:
public int[][] ArrayArray(int arraySize, int randomNumber) {
Random gen = new Random(randomNumber);
int[]ten= new int[10];
int[] hundred= new int[100];
int[]thousand= new int[1000];
int[]tenThousand= new int[10000];
int[][] array = {ten,hundred,thousand,tenthousand};
return array;
}
notice it is int[][] and not int[]. this is because the array you are trying to return a two dimensional array not just a single dimensional array.
let me know how this works.
If you are trying to create array of arrays then multi dimensional array is the way to go.
int arraySize = 10;
int[]ten = new int[arraySize];
int[] hundred = new int[arraySize];
int[]thousand = new int[arraySize];
int[]tenThousand = new int[arraySize];
int[][] array = new int[4][arraySize];
array[0] = ten;
array[1] = hundred;
array[2] = thousand;
array[3] = tenThousand;
Also passing around a 2-dimensional array is same as arrays. Eg.
public static int hello(int[][] pass) {
return pass;
}