[go: up one dir, main page]

0% found this document useful (0 votes)
35 views16 pages

Lab 1 - Arrays

Arrays allow storing and accessing a sequence of objects of the same type. They use consecutive memory locations to store their elements. An array element's index is used to pinpoint its position. Common array operations include searching, insertion, deletion, traversal, sorting, merging, and reversing elements. Arrays are declared with a type followed by square brackets and the array name. Individual elements are accessed via the array name and index. Loops are often used to iterate through and process each element of an array.

Uploaded by

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

Lab 1 - Arrays

Arrays allow storing and accessing a sequence of objects of the same type. They use consecutive memory locations to store their elements. An array element's index is used to pinpoint its position. Common array operations include searching, insertion, deletion, traversal, sorting, merging, and reversing elements. Arrays are declared with a type followed by square brackets and the array name. Individual elements are accessed via the array name and index. Loops are often used to iterate through and process each element of an array.

Uploaded by

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

ARRAYS

Arrays
• An array is an aggregate data structure that is
designed to store a group of objects of the same
types.
• The array is the most efficient data structure for
storing and accessing a sequence of objects.
• Array: a set of pairs (index and value)
• For each index, there is a value associated with
that index.
• implemented by using consecutive memory.
Arrays
• Arrays are…
• complex variables that can hold multiple values of the same data
type.
• The size declarator indicates…
• the number of elements the array can hold.
• Individual values inside of an array are called…
• elements.
• An element’s index (or subscript) is…
• a uniquely identifying number that is used to pinpoint its position in
the array.
Operations Performed on Array

• Search:- finding a location of an element in the


given value.
• Insertion:- adding new element in array.
• deletion:- removing an element in array.
• Traversal:- processing each element in the
array.
• Sorting:- arranging element in array.
• Merging:- combining two array in single array.
• Reversing:- reversing the element of array
Syntax
• Forms
ElementType [] arrayName =new ElementType
[size];
ElementType [] arrayName = array-
literal;
• Where:
• ElementType is any type
• arrayName is the handle for the array
• array-literal is a list of literals enclosed in curly braces
{ }

• String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};


• System.out.println(cars[0]);
Pgm 1: Access the Elements of an
Array
import java.util.*;
public class pgm1
{
public static void main(String[] args)
{
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
System.out.println(cars[0]);
}
}
Pgm 2:-Change an Array Element
package Java;
import java.util.*;
public class pgm2
{
public static void main(String[] args)
{
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
cars[0] = "Opel";
System.out.println(cars[0]);
}
}
Pgm 3:-Array Length
package Java;
import java.util.*;
public class pgm3
{
public static void main(String[] args)
{
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
System.out.println(cars.length);
}
}
String[] cars;

Arrays
• Java arrays are objects
• must be accessed via handles
• Defining an array
Type [] name
•This declares the handle only
• Where: •Initialized to null
•Stores an address when arrays are created
• Type specifies the kind of values the array stores
• the brackets [ ] indicate this is an array
• name is the handle to access the array

• String[] cars;
Definitions Using Array Literals
• Used when exact size and initial values of an array are
known in advance
• used to initialize the array handle

int [] count = { 10,20,30,40}


Visualize the results of the above command

count [0] [1] [2] [3]


10 20 30 40
Definitions Using Array Literals

final String [] STUDENTS = { “Ali", "Das" …


• Note results: “Rosy"};

STUDENTS [0] [1] [2] [6]

Ali Das. . . Rosy


• As we all know final variable declared can only be
initialized once whereas the reference variable once
declared final can never be reassigned as it will start
referring to another object which makes usage of the final
impracticable.
• final int[] arr = { 1, 2, 3, 4, 5 };
• arr[4] = 1;
1
2
3
4
1
Processing Array Elements

• Access individual elements of an array using:


• the name (handle) of the array
• a number (index or subscript) that tells which of the
element of the array

count [0] [1] [2] [3]


5 -7 9 27

• What value is stored in count[2]?


Processing Array Elements
• Often a for( ) loop is used to process each of the
elements of the array in turn.

• int[] highScores = { 10, 9, 8, 11};


• for (int i = 0; i < highScores.length; i++)
•{
• System.out.println( highScores[i] );
•}
• The loop control variable, i, is used as the index to
access array components
Pgm 4:- Loop Through an Array
(Integer)
package Java;
import java.util.*;
public class pgm4
{
public static void main(String[] args)
{
int[] highScores = { 10, 9, 8, 11};
for (int i = 0; i < highScores.length; i++)
{
System.out.println( highScores[i] );
}
}
}
Pgm 5:-Loop Through an Array
(Strings)
package Java;
import java.util.*;
public class pgm5
{
public static void main(String[] args)
{
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
for (int i = 0; i < cars.length; i++)
{
System.out.println(cars[i]);
}
}
}

You might also like