When debugging, novices insert corrective code; experts remove defective code. Richard Pattis

Arrays

Language Java | Level Intermediate | Category Java collections framework (JCF) | 2015-07-21 20:09:08

1 Comments


Definition

The array is a container which hold the same type of objects or primitive types with fixed length of size.

Character

Array position starts from 0 and end with size-1.
Iterate the array using loop statements (for, while, do-while) or enhanced for loop.
Access the element using an index.
If array index is not available when access the array element, it gives array out of bound exception.
Arrays have fixed length size. The array memory allocated while running the code with a new operator.
A multidimensional array is an array whose components are themselves arrays.
Array can be copied using utility methods or allocate, copy each element from one array to another array.
If we remove the array element in middle, we have to rearrange the array.

source Code

								
package com.careerdrill.collections;

public class Arrays {

	private static int SIZE=5;
	
	public static void main(String[] args) {
		
		int[] array = new int[SIZE];
		
		for(int i = 0; i < SIZE; i++)
			array[i] = i * i;
		
		for(int i = 0; i < SIZE; i++)
			System.out.println(i +" position value " + array[i]);

	}

}
						

Output

    					 
0 position value 0
1 position value 1
2 position value 4
3 position value 9
4 position value 16    					 
					    

Comparison

Array ArrayList
The array is an object. ArrayList is a collection which hold any data type.
Fixed length of data structure. Resizable or dynamic array implementation.
Can contains primitive and wrapper. Cannot contain primitive data types.
Can be multi-dimensional. Can't be multidimensional.
Performance is faster. Slow in comparison.

When to use

When we know the size before creating the container, we can use an array. The dynamic array doubles the memory when the number of element reaches the maximum size.

1 Comments

Posted by selvamselvam
December 7, 2015 6:53 am

hello



Please login to add comments.