0% found this document useful (0 votes)
38 views25 pages

Java12 03

The document discusses Java arrays including one-dimensional and two-dimensional arrays. It covers declaring, instantiating, initializing arrays and accessing array elements. Relational operators and converting string arguments to other types are also discussed.

Uploaded by

metallicgirl
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)
38 views25 pages

Java12 03

The document discusses Java arrays including one-dimensional and two-dimensional arrays. It covers declaring, instantiating, initializing arrays and accessing array elements. Relational operators and converting string arguments to other types are also discussed.

Uploaded by

metallicgirl
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/ 25

PAULO ARAÚJO JAVA 12

[email protected]
SEPTEMBER 2021 SESSION 03

Paulo Araujo - [email protected] 1


JAVA 12
SESSION 03
PAULO ARAÚJO – [email protected] – SEPTEMBER 2021

Paulo Araujo - [email protected] 2


PLANNING – 12 SESSIONS – 30H – SESSION 03

Paulo Araujo - [email protected] 3


RELATIONAL OPERATORS

Condition Operator Example

Is equal to == int i=1;


(i == 1)
Is not equal to != int i=2;
(i != 1)

Is less than < int i=0;


(i < 1)

Is less than or equal <= int i=1;


to (i <= 1)

Is greater than > int i=2;


(i > 1)

Is greater than or >= int i=1;


equal to (i >= 1)

Paulo Araujo - [email protected] 104 4


INTRODUCTION TO ARRAYS

• An array is a container object that holds a group of values


of a single type.
• A value in the array can be a primitive or an object type.
• The length of an array is established when the array is
created.
• After creation, the length of an array cannot be changed.
• Each item in an array is called an element.
• Each element is accessed by a numerical index.
• The index of the first element is 0 (zero).

Paulo Araujo - [email protected] 105 5


ONE-DIMENSIONAL ARRAYS

Example:

• int ageOne = 27;


• int ageTwo = 12;
• int ageThree = 82;
• int ageFour = 70;
• int ageFive = 54;
• int ageSix = 6;
• int ageSeven = 1;
• int ageEight = 30;
• int ageNine = 34;
• int ageTen = 42;

Paulo Araujo - [email protected] 106 6


CREATING ONE-DIMENSIONAL
ARRAYS
Array of int types

27 12 82 70 54 1 30 34

Array of Shirt types

Array of String types

Paulo Araujo - [email protected] 107 7


ARRAY INDICES AND LENGTH

Array ages of eight elements

First Element
index at index 5

00 1 2 3 4 5 6 7 Indices

27 12 82 70 54 1
1 30 34

Array length is 8
(ages.length)

Paulo Araujo - [email protected] 108 8


DECLARING A ONE-DIMENSIONAL ARRAY
• Syntax:

type [] array_identifier;

• Declare arrays of types char and int:


char [] status;
int [] ages;

• Declare arrays of object references of types Shirt and


Shirt [] shirts;
String:
String [] names;

Paulo Araujo - [email protected] 109 9


INSTANTIATING A ONE-DIMENSIONAL ARRAY

• Syntax:

array_identifier = new type [length];

• Examples:

status = new char [20];


ages = new int [5];

names = new String [7];


shirts = new Shirt [3];

Paulo Araujo - [email protected] 110 10


INITIALIZING A ONE-DIMENSIONAL ARRAY

• Syntax:

array_identifier[index] = value;

• Set values in the ages array:


ages[0] = 19;
ages[1] = 42;
ages[2] = 92;
ages[3] = 33;

• Set references to Shirt objects in the shirts array:


shirts[0] = new Shirt();
shirts[1] = new Shirt();
shirts[2] = new Shirt();

Paulo Araujo - [email protected] 111 11


DECLARING, INSTANTIATING, AND
INITIALIZING
ONE-DIMENSIONAL ARRAYS
• Syntax:
type [] array_identifier = {comma-separated list of values
or expressions};

• Examples:
int [] ages = {19, 42, 92, 33, 46};
Shirt [] shirts = {new Shirt(), new Shirt(), new Shirt()};

• Not permitted (Eclipse will show an error):

int [] ages;
ages = {19, 42, 92, 33, 46};

Paulo Araujo - [email protected] 112 12


ACCESSING A VALUE WITHIN AN ARRAY

• Setting a value:

status[0] = '3';
names[1] = "Fred Smith";
ages[1] = 19;
prices[2] = 9.99F;

• Getting a value:

char s = status[0];
String name = names [1];
int age = ages[1];
double price = prices[2];

Paulo Araujo - [email protected] 113 13


STORING ARRAYS IN MEMORY

char size = 'L'


char[] sizes = {'S', 'M', 'L' };

Primitive variable
of type char

0x034009
size L 0 S
1 M
sizes 0x034009
2 L

Primitive variable
of type char held
as array element

Paulo Araujo - [email protected] 114 14


STORING ARRAYS OF REFERENCES IN
MEMORY
Shirt myShirt = new Shirt();
Shirt[] shirts = { new Shirt(), new Shirt(), new Shirt() };

0 shirtID
myShirt 0x034009 0.0 price
U colorCode

shirts 0x99f311 0 shirtID


0x99f311 price
0.0
0x00099 U colorCode
0x00327
0 shirtID
0x00990
0.0 price
U colorCode

0 shirtID
0.0 price
U colorCode

Paulo Araujo - [email protected] 115 15


USING THE ARGS ARRAY IN THE MAIN METHOD

• Parameters can be typed on the command line:

> java ArgsTest Hello World! The second


args[0] is Hello parameter goes into
args[1] and so on.
args[1] is World!
The first parameter
goes into args[0].

• Code for retrieving the parameters:

public class ArgsTest {


public static void main (String[] args) {
System.out.println("args[0] is " + args[0]);
System.out.println("args[1] is " + args[1]);
}
}

Paulo Araujo - [email protected] 116 16


CONVERTING STRING ARGUMENTS TO OTHER
TYPES
• Numbers can be typed as parameters:
> java ArgsTest 2 3
Total is: 23 Concatenation, not addition!
Total is: 5

• Conversion of String to int: These are Strings!

public class ArgsTest {


public static void main (String[] args) {
System.out.println("Total is: " + (args[0] + args[1]));

Integer.parse
int arg1 = Integer.parseInt(args[0]); Int() converts to
int arg2 = Integer.parseInt(args[1]); int.
System.out.println("Total is: " + (arg1 + arg2));
}
Note
Note parentheses.
parentheses
}

Paulo Araujo - [email protected] 117 17


DESCRIBING TWO-DIMENSIONAL
ARRAYS

Wednesday

Thursday

Saturday
Tuesday
Monday
Sunday

Friday
Week 1

Week 2

Week 3

Week 4

Paulo Araujo - [email protected] 118 18


DECLARING A TWO-DIMENSIONAL
ARRAY
• Syntax:
type [][] array_identifier;

• Example:
int [][] yearlySales;

Paulo Araujo - [email protected] 119 19


INSTANTIATING A TWO-
DIMENSIONAL ARRAY
• Syntax:
array_identifier = new type [number_of_arrays] [length];

• Example:

// Instantiates a 2D array: 5 arrays of 4 elements each


yearlySales = new int[5][4];

Quarter 1 Quarter 2 Quarter 3 Quarter 4


Year 1
Year 2
Year 3
Year 4
Year 5

Paulo Araujo - [email protected] 120 20


INITIALIZING A TWO-DIMENSIONAL
ARRAY
Example:
• yearlySales[0][0] = 1000;

• yearlySales[0][1] = 1500;

• yearlySales[0][2] = 1800;

• yearlySales[1][0] = 1000;

• yearlySales[3][3] = 2000;
Quarter 1 Quarter 2 Quarter 3 Quarter 4
Year 1 1000 1500 1800
Year 2 1000
Year 3
Year 4 2000
Year 5

Paulo Araujo - [email protected] 121 21


ARRAYLIST CLASS

• Arrays are not the only way to store lists of related data:
• ArrayList is one of a number of list classes.
• It has a set of useful methods for managing its elements:
• add(), get(), remove(), indexOf(), and many others
• You do not need to specify a size when you instantiate an ArrayList:
• As you add more elements, the ArrayList grows as necessary.
• You can specify an initial capacity, but it is not mandatory to do so.
• An ArrayList can store only objects, not primitives.

Paulo Araujo - [email protected] 122 22


CLASS NAMES AND THE IMPORT STATEMENT

• ArrayList is in the package java.util.


• To refer to the ArrayList in your code, you can fully qualify
java.util.ArrayList myList;
or you can add the import statement at the top of the class.

import java.util.ArrayList;
public class ArrayListExample {
public static void main (String[] args) {
ArrayList myList;
}
}

Paulo Araujo - [email protected] 123 23


WORKING WITH AN ARRAYLIST

Declare a reference.
ArrayList myList;

myList = new ArrayList(); Instantiate the ArrayList.

myList.add("John");
myList.add("Ming"); Initialize the ArrayList.
myList.add("Mary");
myList.add("Prashant");
myList.add("Desmond");

myList.remove(0);
Modify the ArrayList.
myList.remove(myList.size()-1);
myList.remove("Mary");

System.out.println(myList);

Paulo Araujo - [email protected] 124 24


PRACTICE 03

• Creating a Class with a One-Dimensional Array of Primitive Types


• Creating and Working With an ArrayList
• Using Runtime Arguments and Parsing the args Array
• Exercises

Paulo Araujo - [email protected] 125 25

You might also like