KEMBAR78
Unit II | PDF
0% found this document useful (0 votes)
11 views7 pages

Unit II

Uploaded by

Thiru
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views7 pages

Unit II

Uploaded by

Thiru
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Unit-II

Arrays
• Related data item stored in common name is called array.
• Array of primitive
• int[] numbers = {1, 2, 3, 4, 5}; // Array of primitive int values
• char[] letters = {'a', 'b', 'c'}; // Array of primitive char values
• Array of non-primitive
• String[] names = {"Alice", "Bob", "Charlie"}; // Array of String objects
(references)
• Integer[] scores = new Integer[3]; // Array of Integer objects
(references) scores[0] = 100;
Arrays
One dimensional Arrays
• // Declaration
• int[] numbers;
• // Declaration and initialization with a specific size
• int[] ages = new int[5]; // An array of 5 integers
• // Declaration and initialization with values
• String[] names = {"Alice", "Bob", "Charlie"};
One dimensional Arrays
• public class onedimen {
• public static void main(String[] args) {
• // initializing array
• int[] arr = { 40,55,63,17,22,68,89,97,89};
• // size of array
• int n = arr.length;
• // traversing array
• for (int i = 0; i < n; i++)
• System.out.print(arr[i] + " ");
• }}
• Out put
• 40 55 63 17 22 68 89 97 89
Dimensional Arrays
• Examples:
• // Two dimensional array:
• int[][] arr2d = new int[3][5];
• // Three dimensional array:
• int[][][] arr3d = new int[3][5][7];
Two dimensional Arrays
• // Java Program to demonstrate the use of
• // Two Dimensional Array
• import java.io.*;
• class Main {
• public static void main(String[] args){
• // Array Intialised and Assigned
• int[][] arr = { { 1, 2 }, { 3, 4 } };
• // Printing the Array
• for (int i = 0; i < 2; i++){
• for (int j = 0; j < 2; j++)
• System.out.print(arr[i][j]+" ");
• System.out.println();
• }
• }
• }
• OUT PUT
12
34

You might also like