0% found this document useful (0 votes)
26 views

Lec 9 Arrays and ArrayList

The document discusses arrays and ArrayLists in Java. It covers defining and initializing multidimensional arrays, printing 2D arrays using enhanced for loops, passing 2D arrays as method parameters, using variable argument lists, parsing command line arguments, common array manipulations using the Arrays utility class, and introducing the ArrayList class as a resizable alternative to arrays. It also describes adding, removing, finding, and clearing elements in an ArrayList.

Uploaded by

Arfaat Sanitary
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Lec 9 Arrays and ArrayList

The document discusses arrays and ArrayLists in Java. It covers defining and initializing multidimensional arrays, printing 2D arrays using enhanced for loops, passing 2D arrays as method parameters, using variable argument lists, parsing command line arguments, common array manipulations using the Arrays utility class, and introducing the ArrayList class as a resizable alternative to arrays. It also describes adding, removing, finding, and clearing elements in an ArrayList.

Uploaded by

Arfaat Sanitary
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Arrays And Arraylist

Chapter: Java OOP Programming

2nd semester bs cs | Object oriented programming | Title Slide


Multidimensional Arrays In Java

- Defining multidimensional arrays


- Arrays of arrays with data in rows and columns,
like a table

- Syntax for array declaration


- int[][] arrayName; for a 2D array

- Initializing a 2D array example


- int[][] matrix = {{1, 2}, {3, 4}}; creates a 2x2
matrix
Printing 2D Array using Enhanced Loop
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};

// Print the 2D array using an enhanced for loop


for (int[] row : matrix) {
for (int num : row) {
System.out.print(num + " ");
}
Passing 2D Array to a Method/Function
Variable Length Argument Lists In Java

- Understanding varargs
- Varargs allow methods to accept zero or multiple arguments.

- Syntax for varargs


- Use ellipsis (...) to denote a vararg parameter:
methodName(DataType... varName)

- Rules for using varargs


- Varargs must be the last parameter, and only one vararg per
method.

- Example: Varargs in methods


- Define a method printAllStrings(String... strings) that prints
all input strings.
Command Line Arguments In Java

- Understanding command line arguments


- Arguments passed to a program at the time of execution.

- How to read arguments in Java


- Use 'String[] args' in the main method to access arguments.

- Practical example of parsing


- Parse 'args' to appropriate data types as needed.

- Utilizing arguments in programs


- Enhance program flexibility by using arguments for input.
Common Array Manipulations In Java

- Overview of java.util.Arrays
- A utility class with static methods for array operations

- Methods: sort, search, fill, copy


- Use Arrays.sort() for ordering
-Arrays.binarySearch() for finding elements
- Arrays.fill() to populate
-Arrays.copyOf() for creating copies.
-Copies the specified array,
truncating or padding with
zeros (if necessary) so the copy has the specified
length.

- Coding with Arrays class


- Example: int[] nums = {3, 6, 1, 4};
Arrays.sort(nums); // Now nums is {1, 3, 4, 6}
Array List
Introduction To Arrays And Arraylists In Java

- Role of Arrays & ArrayLists in Java OOP


- Arrays and ArrayLists store elements for easy access and
manipulation within Java programs.

- Static vs Dynamic structures


- Static structures have a fixed size, while dynamic structures
can grow or shrink as needed.

- Arrays: Fixed-size data holders


- Arrays have a predetermined size and cannot be altered once
defined.

- ArrayLists: Resizable arrays


- ArrayLists can dynamically adjust their size, allowing
elements to be added or removed.
• Lives in import java.util.ArrayList;
Introduction To Arraylist In Java

- Explore the ArrayList class


- A resizable array, part of the Java Collections Framework.

- Advantages of ArrayList vs arrays


- ArrayLists can grow dynamically, allow duplicate elements,
and provide easy insertion/removal.

- ArrayList's type flexibility

- Using generics to specify type

the compiler encounters the diamond (<>) in the class


instance creation expression, it uses the declaration of
variable items to determine the ArrayList’s element type
(String)—this is known as inferring the element type
• - Consume only the required memory
Adding Elements To Arraylist

- Utilize add() method


- add(element) appends to the end

- Insert at specific positions


- Use add(index, element) to place elements at the
desired index.

- ArrayList auto-resizing
- ArrayList grows automatically as you add elements.
Clearing An Arraylist In Java

- Understanding clear() method


- clear() removes all elements, leaving list empty.

.
Removing Elements From Arraylist

- remove() by index
- Removes element at specified index, e.g., list.remove(2)

- remove() by object
- Removes first occurrence of object, e.g.,
list.remove("apple")

- Shifts after element removal


- Subsequent elements shift left to fill the gap
Finding Elements In Arraylist

- Understanding indexOf() method


- Returns the index of the first occurrence of an element

- Locating first occurrence index


- If element exists, returns its index position

- Handling non-existent elements


- Returns -1 if the element is not in the list

- Practical usage of indexOf()


- Useful for searching in lists and data manipulation
Arraylist'S Tostring Method

- Understanding toString() method


- toString() returns a readable String
representation of an object

- Converting ArrayList to String


- Use toString() to get a comma-separated list of
elements in an ArrayList

You might also like