#practical usage of one dimentional array
Explore tagged Tumblr posts
techandguru-blog · 6 years ago
Link
Did you see a basket full of apples? What did you notice? 1. All the items in the basket are apple (same type). 2. All items are present side by side (continuous memory/space) and 3. The size of the basket is fixed and predefined i.e. size of basket can not auto expand to accommodate more items than its capacity. In the same way, in Java, we have Java arrays. The array is a collection of same type items. The size of the array is defined at declaration and can not change after that. Hope you got the idea of array pretty much. In this post, we will learn Java arrays and array usages in detail.
SO WHAT IS AN ARRAY?
So as defined earlier, an array is a fixed size collection of items of the same type. Here are important points about an array:
- All items in the array are of the same type
 - An array can have duplicate items
- Size of an array is fixed and defined at instantiating time.
- Depending upon memory allocation, items are present in continuous memory space.
- Memory allocation to array is dynamic in Java i.e. memory is allocated automatically.
- Items in the array are ordered
- The array is a direct subclass of Object
- Every Array implementation implements Cloneable and Serializable interface.
- An array can be declared using "[]". e,g String[] arrayVar;
- An array can store primitive and object type both
- If the array store object then the actual objects are allocated memory in HEAP
Tumblr media
Java Array and Indexing
CREATING, INITIALISING AND ACCESSING AN ARRAY
ONE DIMENSION ARRAY:
One dimensional array is can be declared like 
type var-name[]; OR type[] var-name;
Array declaration contains type and variable name. Type of array tells which kind of items can be stored in the array. The type could be primitive or Object. Let's see examples of array declaration
byte[] byteArr;  //byte array
int[] intArr;   //can store int only
long[] longArr; // can store long
String[] stringArray;  //can store strings only
Object[] objectArray; //can store items of type objects..literally any type in java
Above arrays are declared only i.e. such variable are not given any memory and nothing is actually existing for them. It just tells the compiler that such items/variables are declared. 
INSTANTIATING AN ARRAY
- declaring an array just create reference only but no memory allocation. Proces of allocating memory to array is called array instantiation.
- an array can be instantiated like e.g.  Type[] arr = new Type[sizeofarra];
- Type tells the type of array items
- sizeofarra is the size of the array it tells how many items can be stored in the array at max
- Index of items in an array starts at 0 and goes till (size of the array - 1)
e.g. 
intArr = new int[20]; // allocating memory to the array intArr declared above - default values of array items are 0, false and null for Number type, boolean and object respectively.
INITIALIZING ARRAY WITH VALUES AT DECLARATION: ARRAY LITERAL
- The array can be declared and initialized using
int[] arr = {1,2,3,4,5,6,78,8};
- Above statement, does declaration, instantiation, and initialization automatically. You need not specify "new type[size]" etc.
- arrays in java can be easily iterated using loops and even Java above 5 provide optimized syntax to traverse array item like below
foreach(Type item in items){  //items is an array of type Type //can manipulate item here  }
- If an attempt is made to access elements outside the array, you will encounter ArrayIndexOutOfBoundsException on runtime.
MULTIDIMENSIONAL ARRAY
- An array could be multidimensional i.e. each item of an array will contain a reference to one dimension lower array as that of a parent. e.g. int[][][] items is 3-D array; So items[0] will refer to int[][] array and items[0][0] will refer to int[] and item[1][0][0] will actually contain int value.
Tumblr media
Java Multidimensional Array
- an array can be passed as an ARGUMENT OF THE FUNCTION USING ARRAY VARIABLE NAME ONLY.
- an array can be returned from a function using an array variable name.
- an array can contain the object of any valid java type or user-defined.
CLONING A JAVA ARRAY
an array can be cloned. Cloning is a process to deep copy I,e means an exact replica of the original array is created and cloned instance contains the exact item rather than the reference of the parent array items.
e.g. int[] arra = new int[10].
int[] clonedarra = arra.clone();
now arra and clonearra are two different arrays in memory with exactly the same items but a different copy.
USAGES:
- the array can be used when we know the max items to be stored.
- when items to be stored are of the same type
You may like to visit other Java Basic Concepts
Oh! you reached the end of the article. Hope you like, please share, subscribe and comment.
0 notes