Having trouble declaring arrays in java

Asked By 50 points N/A Posted on -
qa-featured

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

SHARE
Best Answer by Sheldon Ron
Best Answer
Best Answer
Answered By 0 points N/A #102248

Having trouble declaring arrays in java

qa-featured

 

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 10 points N/A #102249

Having trouble declaring arrays in java

qa-featured

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

Related Questions