All About Array

All About Array

  • What is Array:

Array is group of primitive data, group of objects. Array is group of similar data (homogeneous data). Array offers convenient means of grouping related information. Before going ahead we should know some concept about Array: Fixed in size, Indexd based, Index always start from 0, for creating array we must know data type and size of array.

  • How to declare Array

  1. One Dimensional Array

int [ ] x;         

above syntax is highly recommended because name is clearly separated by type.

int [6] x;        

above syntax is not allowed in java. You can't specify the size of array while array declaration.

2. Multi Dimensional Array

int [ ] [ ] x;
int x[ ] [ ] ;
int [ ] [ ] x;
int [ ] x [ ];
int [ ] x [ ];        

All are valid

  • Array Creation

  1. 1 D Array

int [ ] a = new int [3];        

In java new keyword is used to create object of class. Here also we are using new keyword. There are predefined java classes for each data type. In above code we are creating array of integer element.

a.getClass().getName();        

When we execute above line the the output is: [ I I means Integer.

Array Type Class

int [] [I

double[] [D

short[] [S

byte[] [B

boolean[] [Z

2. 2D Array Creation

In java there is no concept of matrix, instead of this java uses array of arrays.

This concept is used to memory utilization. In matrix some blocks of memory could be wasted.

int [ ] [ ] x = new int [2] [ ]
x[0] = new int [2];
x[1] = new int [3];
x[2] = new int[2];        

  • Annonymous Array

Sometimes we can declare an array without name such type of nameless arrays are called as annonymous array.

Purpose of Annonymous array: Just for instant use(One time use only)

sum(new int [ ] {10,45,67});        

while creating annonymous array we can't specify size of array.

Prof. Dr. Manoj Sathe

Associate Professor - Computer Science

3 个月

More Knowledge…. More Strength

要查看或添加评论,请登录

Devidas Sabale的更多文章

  • Data Types in Java

    Data Types in Java

    Java is a strongly typed programming language. Therefore, you have to mention the type of value before compilation…

  • Var arg method in Java

    Var arg method in Java

    Prior to learning this concept, I used to think that we could not change the syntax of the main method. But we can make…

  • What is literal in Java?

    What is literal in Java?

    Literals are the constant values that can be assigned to a variable. Types of literals: Decimal Literal Octal Literal…

    2 条评论
  • Why Java is not a fully object oriented programming language?

    Why Java is not a fully object oriented programming language?

    Here are reasons: Primitive Data Types: In Object Oriented Programming everything should be object. But in Java there…

社区洞察

其他会员也浏览了