Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Showing posts with label ARRAY. Show all posts
Showing posts with label ARRAY. Show all posts

Friday, August 4, 2017

Difference Between Array and Collection In Java

Difference Between Array and Collection

Difference Between Array and Collection

Now here we are going to discuss what is the difference between array and collection in java. 

So there are many differences between these two concepts i.e array and collection.

Arrays vs collections in java is also a very important java interview question which is mostly asked.

So without wasting time, let's start


Array

  • Array is a collection of similar types(i.e similar data types) of elements with the contiguous memory location.
  • Array is an object in java.
  • Java Array is fixed in size i.e the size of the array cannot be increased or decreased once when it is declared.
  • We can store both primitive types and objects in an array.
  • Array consumes more memory.
  • Performance point of view arrays faster than a collection.
  • There is no predefined method for the array available.
  • Array can hold only homogeneous elements.


Simple Java Array Example

public class Demo
{
public static void main(String args[])
{
//declaration, instantiation, initialization of an array
int a[] = {5, 7,3,8,1,9,6,4,2};
for(int i = 0; i<a.length; i++)
{
System.out.println(a[i]);
}
}
}

Output : 5
              7
              3
              8
              1
              9
              6
              4
              2


Collection

  • Collection allows to a group of an object to be treated as a single unit.
  • Collection is a framework or interface in java.
  • Collection is dynamic in nature i.e collection can grow(increase or decrease) their size as needed.
  • Collection can only store objects. It doesn't store primitive data types.
  • Collection consumes less memory in comparison of arrays.
  • Performance point of views collection is not faster than an array.
  • There are predefined methods are available for collection.
  • Collection can hold both homogeneous and heterogeneous elements.


There are many classes which implement the collection interface like ArrayList class, LinkedList class, HashSet class, and LinkedHashSet class etc. But here we will take an example of ArrayList class.



Java ArrayList Example - Collection

import java.util.*;
public class Test
{
public static void main(String args[])
{
//declaring ArrayList 
ArrayList<String> al = new ArrayList<String>();
al.add("Seema");//insert elements
al.add("Baby");
al.add("Sid");
al.add("Tara");
al.add("Seema");
al.add("Gudia");

//traverse the elements
Iterator i = al.iterator();
while(i.hasNext())
{
System.out.println(i.next());
}
}
}

Output : Seema
              Baby
              Sid
              Tara
              Seema
              Gudia


Similarities Between Array and Collection

According to my point of view, The similarities between array and collection are that both can work on the index basis. For example

In an array, we can access any elements at any index position. Java array provides us random access by using index position. The index positions start with 0 index.

In the collection, we can get any element by using index number. There are some classes of the collection framework which works at index basis.


Which is Better Array or Collection

This is very difficult to say which is better array or collection but performance wise array is better than a collection. And with respect to memory collection is better than array. So there are some places array is better and some place collection is better.

Read More:

Difference Between Abstract Class and Interface in Java.
Difference Between Class and Interface in Java.
Difference Between Constructor and Method in Java.
Difference Between String and StringBuffer.
Difference Between C++ and Java.
Difference Between ArrayList and Vector in Java.

Share:

Thursday, March 23, 2017

Java Array

 Array

Array is a collection of similar types of elements or data that have contiguous memory location.

Array is an object in java. In java, array is represented via an object. Java array is a data structure where we store similar data types. Java array is fixed in size i. e you cannot increase the size of array at runtime. You can store only fixed set of elements in a java array.

Java array is indexed based, first elements of the array is stored at 0 index.
Array indexe


Advantage of Java Array

  • Code Optimization : There is no need to declare a lot of variable of same data types. We can retrieve and sort data easily.
  • Random Access : In an array, we can get any data located at any index position.
  • Performance : The performance of an array is good because you can search any element in an array on the basis of index.


Disadvantage of Java Array

  • Size Limit : Once an array has got a memory then that memory can't increase of decrease at runtime.
To solve this problem there is ''collection'' framework in java. Collection framework is an alternate of java array.


Types of Array

There are two types of array in java.

  1. Single Dimensional Array (e.g dataType[] var;)
  2. Multi Dimensional Array (e.g dataType[][] var;)

Single Dimensional Array in Java

Array Declaration in Java

Single dimensional array declaration.
dataType[] var;
        or
dataType []var;
        or
dataType var[];


Instantiation of an Array in Java

Array in java is an object, Hence we can create object by new keyword.

dataType var[] = new dataType[size];//instantiation and size of array 


Example of Single Dimensional Array in Java

In the below example, we are going to declare, instantiate, initialize and traverse an array.

For example:

class Numbers
{
public static void main(String args[])
{
int a[] = new int[5];//declaration and instantiation 

a[0] = 1;//initialization
a[1] = 2;
a[2] = 3;
a[3] = 4;
a[4] = 5;

//print array

for(int i = 0; i<a.length; i++)//length is the property of array
{
System.out.println(a[i]);
}
}
}

output :1
              2
              3
              4
              5


Java Array Declaration, Instantiation, and Initialization

You can declare, instantiate and initialization an array in one step e.g

int a[] = {10,20,30,40,50};//declaration, instantiation, initialization 

For example

class Numbers
{
public static void main(String args[])
{
int number[] = {99,100,80,40};//declaration,instantiation,initialization
for(int i = 0; i<number.length; i++)
{
System.out.println(number[i]);
}
}
}

output: 99
             100
             80 
             40


Accessing Array Elements in Java

You can access the elements of array by using the index value of the element.

For example:

class Test
{
public static void main(String args[])
{
int a[] = {2,4,6,8,1};
System.out.println(a[3]);
}
}

output: 8


Multidimensional Array in Java

In the multidimensional array, data is stored in row and column based index(also known as matrix form).


Multidimensional Array Declaration in Java

dataType[][] var;
          or
dataType [][]var;
          or
dataType var[][];
          or 
dataTytpe []var[];


Instantiation of Multidimensional Array in Java

int var[][] = new int[3][3];//3 rows and 3 column 

Initialization of Multidimensional Array in Java

var[0][0] = 1;
var[0][1] = 2;
var[0][2] = 3;
var[1][0] = 4;
var[1][1] = 5;
var[1][2] = 6;
var[2][0] = 7;
var[2][1] = 8;
var[2][2] = 9;


Example of Multidimensional Array in Java

In the below example, we are going to declare, instantiate, initialize and print multidimensional array in java.

For example:

class Numbers
{
public static void main(String args[])
{
int a[][] = { {1,2,3}, {4,5,6},{7,8,9}};//dec..insta..initiz

//print 2d array

for(int i =0; i<3; i++)
{
for(int j =0; j<3; j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
} 

output: 1 2 3
             4 5 6
             7 8 9


How to find class name of Java Array

Array is an object in java. For array object , proxy class is created whose name can be obtained by getClass() and getName() methods on the object.

For example:

class ArrayName
{
public static void main(String args[])
{
int arr[] = {1,2,3};
class c = arr.getClass();
String s = c.getName();
System.out.println(s);
}
Share:

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Blog Archive

Translate