0% found this document useful (0 votes)
3 views4 pages

COS202 Lecture 3

This document provides an overview of Java arrays, detailing their structure, declaration, creation, and access methods. It explains how to declare an array, create it using the 'new' operator, and access its elements using index numbers. Additionally, it includes examples of processing arrays with for and for-each loops, along with classwork and assignment tasks related to array manipulation.

Uploaded by

wwwtope947
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)
3 views4 pages

COS202 Lecture 3

This document provides an overview of Java arrays, detailing their structure, declaration, creation, and access methods. It explains how to declare an array, create it using the 'new' operator, and access its elements using index numbers. Additionally, it includes examples of processing arrays with for and for-each loops, along with classwork and assignment tasks related to array manipulation.

Uploaded by

wwwtope947
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/ 4

ACHIEVERS UNIVERSITY, OWO

COLLEGE OF NATURAL AND APPLIED SCIENCES


DEPARTMENT OF COMPUTER SCIENCE
COS 202 – COMPUTER PROGRAMMING II – 3 UNITS

LECTURER IN CHARGE - MR. ADEPOJU, S. E.

LECTURE 3

JAVA ARRAY
Introduction
Data Structure is a systematic way to organize data in order to use it efficiently. Data Structure is
about rendering data elements in terms of some relationship, for better organization and storage.
For example, we have some data which has player's name "Alexa" and age 26. Here "Alexa" is
of String data type and 26 is of integer data type.
Java provides a data structure, the array, which stores a fixed-size sequential collection of
elements of the same type. An array is used to store a collection of data, but it is often more
useful to think of an array as a collection of variables of the same type. Instead of declaring
individual variables, such as number0, number1, ..., and number99, you declare one array
variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent
individual variables.

Declaring Array
To declare an array, define the variable type with square brackets and variable to reference the
array.
Syntax
​ dataType[] variableName;​ // preferred way.
or
dataType variableName [];​ // works but not preferred way.

Note: The style dataType[] variableName is preferred. The style dataType variableName []
comes from the C/C++ language and was adopted in Java to accommodate C/C++ programmers.

Creating Array
You can create an array by using the new operator with the following syntax:
variableName = new dataType[arraySize];
The above statement does two things
●​ It creates an array using new dataType[arraySize];
●​ It assigns the reference of the newly created array to the variable variableName.

Declaring an array variable, creating an array, and assigning the reference of the array to the
variable can be combined in one statement, as shown below:
dataType[] variableName = new dataType[arraySize];

Alternatively you can create arrays as follows:


dataType[] variableName = {value0, value1,..., valuek};

Accessing Array elements


You can access an array element by referring to the index number. In example 3.1, the statement
accesses the value of the first element in the array.
Note: Array indexes start with 0: [0] is the first element. [1] is the second element, etc.

Example 3.1
1​ public class Main {
2​ public static void main(String[] args) {
3​ String[] fruits = {“Apple”, “Banana”, “Cucumber”, “Lemon”, “Mango”};
4
5​ System.out.println(fruits[0]);
6​ }
7​ }

To find out how many elements an array has, use the length property. Example 3.2 shows the
length keyword being used in the Java program and also, to change the value of a specific
element by referring to the index number.
Example 3.2
1​ public class Main {
2​ public static void main(String[] args) {
3​ String[] fruits = {“Apple”, “Banana”, “Cucumber”, “Lemon”, “Mango”};
4​
5​ fruits[0] = “Avocado”;
6​
7​ System.out.println(fruits[0]);
8​ System.out.println(fruits.length);
9​ }
10​ }
Processing Arrays
When processing array elements, we often use either for loop or for-each loop because all of the
elements in an array are of the same type and the size of the array is known.
To loop through the array elements with the for loop, and use the length property to specify how
many times the loop should run.
Example 3.3
1​ public class Main {
2​ public static void main(String[] args) {
3​ String[] fruits = {“Apple”, “Banana”, “Cucumber”, “Lemon”, “Mango”};
4​
5​ for (int i = 0, i < fruits.length, i++) {
6​ System.out.println(fruits[i]);
7​ }
9​ }
10​ }

There is also a for-each loop, which is used exclusively to loop through elements in arrays.
Syntax
​ for (type variable : arrayname) {
​ …
}

Example 3.4
1​ public class Main {
2​ public static void main(String[] args) {
3​ String[] fruits = {“Apple”, “Banana”, “Cucumber”, “Lemon”, “Mango”};
4​
5​ for (String i : fruits) {
6​ System.out.println(fruits[i]);
7​ }
9​ }
10​ }

Example 3.4 can be read like this: for each String element (called i - as in index) in fruits, print
out the value of i. If you compare the for loop and for-each loop, you will see that the for-each
method is easier to write, it does not require a counter (using the length property), and it is more
readable.
CLASSWORK
1)​ Create an array of 12 elements and display the 5th, 8th and last element of the array
respectively.
2)​ Create a multidimensional array of two containers: Print the 3rd element in the second
container.

ASSIGNMENT
1)​ Using an array, write a Java program that takes 5 integers entered at runtime and displays all
the integers.
2)​ Write a Java program using an array consisting of integer elements: display all the elements
in the array and also display the sum of elements either using the for loop or for-each loop.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy