Array Ni Lee
Array Ni Lee
UC1
❑ Work with Methods and Encapsulation
❑ Work with Inheritance and Handling
Exceptions
❑ Examine Object-Oriented Concepts and
Terminology
❑ Explain Modeling and Software
Development Process
❑ Create Use Case Diagrams and Use Case
Scenarios
❑ Transition Analysis to Design using
Interaction Diagrams
❑ Introduce Architectural Concepts and
Architecture Tiers Diagrams
UC1.1: Apply Basics
of Java Language
1.1.1 Executable Java applications are created in accordance with
Java framework
1.1.2 Java packages are imported to make them accessible in the
code
1.1.3 Working with Java Data types is demonstrated in
accordance with Java framework
1.1.4 Using Operators and Decision Constructs is demonstrated
in accordance with Java framework
1.1.5 Creating and Using Arrays is demonstrated in
accordance with Java framework
1.1.6 Using Loop Constructs is demonstrated in accordance with
Java framework
UC1.1.5: Creating and Using
Arrays is demonstrated in
accordance with Java framework
For example, if we want to store the names of 100 people then we can create an
array of the string type that can store 100 names.
Here, the above array cannot store more than 100 names. The number of values in a
Java array is always fixed.
Representation of an array
We can represent an array in various ways in different programming languages.
As per the above example, there are some of the following important points:
• An element is an individual item or value stored in an array. In the array, the
elements are "Volvo", "BMW", "Ford", and "Mazda".
• The length of an array is the total number of elements it can hold. The array's
length is 4, which means we can store 4 elements.
• Index is the position number of an element in the array. Indices in Java
arrays start at 0 and go up to the array’s length minus 1.
Declare an Array
In Java, here is how we can declare an array:
datatype[] arrayName;
• datatype - it can be primitive data types like int, char, double,
byte, etc. or Java objects
• arrayName - it is an identifier
// allocate memory
data = new double[10];
Initialize an Array
initialize arrays during declaration
Here, we have created an array named cars and initialized it with the
values inside the curly brackets.
Note that we have not provided the size of the array. In this case, the
Java compiler automatically specifies the size by counting the
number of elements in the array (i.e. 4).
Here, we can also initialize arrays in Java, using the index number.
Access the Elements of an
Array
You can access an array element by referring to the index number.
This statement accesses the value of the first element in cars:
System.out.println(cars[0]);
// Now outputs Audi instead of Volvo
Array Length
To find out how many elements an array has, use the length
property:
System.out.println(cars.length);
// Outputs 4
Loop
Through
an Array
For Loop
You can loop through the array elements with the for loop, and use the length
property to specify how many times the loop should run. The following example
outputs all elements in the cars array:
The following example outputs all elements in the cars array, using a "for-each" loop:
The example above can be read like this: for each String element (called i - as in index)
in cars, print out the value of i.
Example 1: Using For Loop
class Main {
public static void main(String[] args) {
// create an array
int[] age = {12, 4, 5};
// loop through the array
1.1.5.a
2. Update the "Saturday" and "Sunday" to "Sat" and
"Sun".
3. Iterate Using a for Loop: The traditional for loop
allows you to access each element in the array by its
index.
4. Iterate Using a for-each Loop: The for-each loop
provides a simplified way to iterate through all the
elements in the array without needing to reference
the index.
Java Multidimensional
Arrays
A multidimensional array is an array of arrays. Each element of a
multidimensional array is an array itself. For example,
Let's take another example of the multidimensional array. This time we will
be creating a 3-dimensional array. For example,
class Main {
public static void main(String[] args) {
// create ArrayList
ArrayList<String> languages = new ArrayList<>();
}
Basic Operations
on ArrayList
The ArrayList class provides various methods to
perform different operations on arraylists. We will look
at some commonly used arraylist operations in this
tutorial:
• Add elements
• Access elements
• Change elements
• Remove elements
Add Elements to
an ArrayList
To add a single element to the arraylist, we use the add() method of
the ArrayList class. For example,
import java.util.ArrayList;
class Main {
public static void main(String[] args){
// create ArrayList
ArrayList<String> languages = new ArrayList<>();
import java.util.ArrayList;
class Main {
public static void main(String[] args){
// create ArrayList
ArrayList<String> languages = new ArrayList<>();
// add() method without the index parameter languages.add("Java");
languages.add("C");
languages.add("Python");
import java.util.ArrayList;
class Main {
public static void main(String[] args){
ArrayList<String> languages = new ArrayList<>();
languages.add("Java");
languages.add("C");
languages.add("Python");
import java.util.ArrayList;
class Main {
public static void main(String[] args){
ArrayList<String> languages = new ArrayList<>();
languages.add("Java");
languages.add("C");
languages.add("Python");
languages.add("Java");
languages.add("C");
languages.add("Python");
languages.add("Java");
languages.add("C");
languages.add("Python");
1.1.5.b
2. Update the "Saturday" and "Sunday" to "Sat" and
"Sun".
3. Use a for-each loop to iterate through the ArrayList
and print each day.
4. Remove "Friday”
5. Repeat step 3
Other ArrayList Operations
Method Description Example
size() Returns the length of the arraylist. int size = languages.size();
1.1.5.c
2. Remove all elements.
3. Check if ArrayList is empty using isEmpty()
Thank You