I am having trouble declaring arrays in java. I don't know what is the code or syntax that can be use in declaring arrays. Please, teach me about arrays in java and how to code it. thanks
Having trouble declaring arrays in java
Â
Array declaration in java is not at all critical.
Â
By the way you need to understand difference between references and objects.
lets take an example,
int num[];
Here num is a reference for array of type integer. In other words it can hold an array.
Now to create the cells of the array you need to allocate the memory.
Simply,
num = new int[10];
Â
Togetherly we can write,
Â
int num = new int[10];
Â
Thus an array of 10 cells of integer type are created.
you can access the array asÂ
num[0]=23;
num[1]=56;
etc…
Â
However If you do not use new, and try to use the array then the error, ‘null’ will be shown. Thus, the simple and straight forward means to declare an array of size 20, type int isÂ
int num = new int[20];
Â
Â
Answered By
sd123123
10 points
N/A
#102249
Having trouble declaring arrays in java
For two dimension array the syntax is quite same like one dimensional array
syntax is
int array_name [ ] = new int [ 3][2];
that means it has 3 rows and 2 column.
You can assign value directly or one by one
to assign directly
array_name[0][0]=2;
or you can take input from use using buffer reader class
Hope this will help