Java Arrays Tutorial part1

Java arrays Notes

coding-zon-single-array-java-part1

PART-2

Table of Contents

  1. Single Dimensional Array
    1. Declaring an array
    2. Initialization of an array
      1.     Direct initialization
      2.     Creating array using new
    3. Print array elements
    4. Length keyword
    5. Printing an array using for-Loop
    6. Printing an array using for-each Loop
    7. char Array
    8. String Array


 What is array?

 A collection of values of similar type, stored in one variable, is called as an array.
 In java, to store multiple values, an array is used.

Arrays are 2 types

  1. Single Dimensional Arrays
  2. Double Dimensional Arrays

1. Single Dimensional Array in Java

 
All the array elements are stored in a single row.

Declaring an array:

syntax:

<datatype> <arrayname>[];
 
example: 

 int n[];

it stores only int values in the n variable.


***we cannot give a size of the array at the time of declaration.
 

 

Initialization of an array

2 types :

  1. Direct initialization

  2. Creating array using new

 


1) Direct initialization
 
 

example: 

int n[] = {5,3,6,2,4};

Array values are separated with ‘,’. They are called elements.



---> 0..1.. are index value of an array element. It indicates the position of an element.
---> array index always starts with 0.


ex:

System.out.println(n[2]);    //6
System.out.println(n[4]);    //4



 Menu

2) Creating array using new in Java

 new is for initializing the size of an array.

 

syn:
int <arrayname>[] = new <datatype>[size];
ex: int n[] = new int[5];
 
 
 
 

Print array elements:

example-1: 
 
public class ArrayDemo{
public static void main(String args[])
{
int n[] = {5,3,6,2,4};
{
System.out.println(n[0]);    //5
System.out.println(n[1]);    //3 
System.out.println(n[2]);    //6
System.out.println(n[3]);    //2 
System.out.println(n[4]);    //4
}
}

 

example-2: 

Printing an array using for-Loop in Java

public class ArrayDemo{
public static void main(String args[])
{
int n[] = new int[5];
n[0] = 6;
n[1] = 7;
n[2] = 5;
n[3] = 4;
n[4] = 9;
for( int i=0;i<5;i++)
{
System.out.println(n[i]);
}
} 

 length  ->  length property returns length of an array.
ex: 


System.out.println(n.length);  //5

o/p: 5
using for..each loop to print array elements

 

Printing an array using for-each Loop in Java

 

 public class ArrayDemo{

public static void main(String args[])
{
int n[] = new int[5]; 
n[0] = 6;
n[1] = 7;
n[2] = 5;
n[3] = 4;
n[4] = 9;
 
for( int i:n)
{
System.out.println(i); 
}
 

 

Character Array in Java

example-1:  
 

public class ArrayDemo{

public static void main(String args[]) {

 char  name[] = new char[3]; 

    name[0] = 'J'; 

    name[1] = 'A'; 

    name[2] =  'V';

    name[3] =  'A';

  System.out.println(name);

 } }

 

String Array in Java

 
example-1:  
 
public class ArrayDemo
{
public static void main(String args[]){ 
 
String[] books= {"Java", "PHP", "HTML","JavaScript"};
 
for (int i=0; i<books.length; i++)
{
 
System.out.println(books[i]);
}
}
} 
 
 
Java
PHP
HTML
JavaScript
 
 
 
Practice
 
Calculate total marks using marks array.
Find evens in a given array
find a given number in the array. Use Scanner.
Count occurrences of a given number in an array.
Print array in reverse. 

 

 

 

Comments

Popular posts from this blog

Using javascript pass form variables to iframe src

Creating a new PDF by Merging PDF documents using TCPDF

Import excel file into mysql in PHP