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

Java Arrays [g2]

The document provides an overview of arrays in Java, detailing their syntax, types (one-dimensional and multidimensional), and various operations such as searching, sorting, adding, and removing elements. It also discusses how to pass arrays to methods and introduces the ArrayList data structure, highlighting its dynamic sizing and advantages over traditional arrays. Additionally, it covers methods for converting ArrayLists to arrays.

Uploaded by

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

Java Arrays [g2]

The document provides an overview of arrays in Java, detailing their syntax, types (one-dimensional and multidimensional), and various operations such as searching, sorting, adding, and removing elements. It also discusses how to pass arrays to methods and introduces the ArrayList data structure, highlighting its dynamic sizing and advantages over traditional arrays. Additionally, it covers methods for converting ArrayLists to arrays.

Uploaded by

jaharah Bercero
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 36

ARRAYS IN JAVA

ARRAYS
Syntax of Array

e. data type identifier =


values;
Types of Array
arrays can be categorized into several types
based on their structure and usage.

Array Operations
various actions that can be
performed on arrays to manipulate
and manage the data they contain.
Passing Array
Methods
manipulate and operate on
collections of data without needing
to return multiple values

Array List
data structure in Java
that works like a
Arrays

Arrays are used to store They are used to hold a


multiple values in a single fixed number of values, and
variable, instead of the size of an array is
declaring separate variables determined when it is
for each value. created.
Syntax of Arrays

Declaring, initializing, and accessing elements of an array


Types of Arrays

One-Dimensional Array
or single dimensional array can be
visualized as a single row or a column of
array elements that are represented by a
variable name and whose elements are
accessed by index values and must deal
with only one parameter.
Multidimensional Array
arranged as an array of arrays. You can look
it as a single container that stores multiple
containers. Each element of a
multidimensional array is an array itself
One-dimensional Arrays

.
Multidimensional Arrays
Array Operations

Adding and removing


Searching Arrays elements
finding a specific element or you can add or remove
a set of elements within the elements by creating a new
array array or using more flexible
data structures like ArrayList
02

Sorting Arrays
arranging the elements of
the array in a specific order
Merging arrays
combining two or more arrays
into a single array
Sorting Arrays

Sorting in arrays refers to the process of arranging the


elements of an array in a specific order, usually ascending
(smallest to largest) or descending (largest to smallest).
Java provides multiple ways to sort arrays:

Ways:
Arrays.sort()
- built in sorting
Bubble Sort

- manual sorting
Sorting Arrays

Example:

.
Searching Arrays

There are numerous approaches to check (search) whether a


specific element is present in this Array or not using:

• Linear Search method

• Binary Search method

• List.contains() method

• Stream.anyMatch() method
Searching Arrays using Linear Search method

In Linear Search, the list or array is traversed sequentially,


and every element is checked.
Syntax:
Searching Arrays using Linear Search method
Searching Arrays using Binary Search Method

Binary search is a method for finding a specific value in a sorted


array by repeatedly dividing the search interval in half until the
value is found or the interval is empty.

Syntax:
Searching Arrays using Binary Search method
Searching Arrays using List.contains() method

List contains() method in Java is used for checking if the


specified element exists in the given list or not.

Syntax:
Searching Arrays using List.contains() method
Searching Arrays using Stream.anyMatch() method

Stream anyMatch(Predicate predicate) returns whether any


elements of this stream match the provided predicate.

Syntax:
Searching Arrays using Stream.anyMatch() () method
Adding and Removing Elements

Adding and removing elements in an array is a little harder because we may


need to shift elements up and down in the array depending on where the item
was added or removed. When adding an element, we also need to have enough
space in the array.
Merging Arrays
Merging Arrays

Example:
Passing Array to Methods

Modifying elements od
an array in a method

Passing arrays as 02
parameters to method

Returning an array
from a method
Passing arrays to method

You can pass arrays to a


method just like normal
variables. When we pass an
array to a method as an
argument, actually the
address of the array in the
memory is passed
(reference). Therefore, any
changes to this array in the
method will affect the array.
Passing arrays as a parameter to method

Arrays can be passed to


other methods just like how
you pass primitive data
type’s arguments. To pass
an array as an argument to
a method, you just have to
pass the name of the array
without square brackets.
The method prototype
should match to accept the
argument of the array type.
Modifying elements of an array in a method

To modify an element in an array, you assign a new value to the


desired index.

Syntax: Example:
Returning an Array from a method

To modify an element in an array, you assign a new value to the


desired index.
Example:
ArrayList

Java ArrayList is a part of the collections framework and it is a class of java.util


package. It provides us with dynamic-sized arrays in Java.

The main advantage of ArrayList is that,


unlike normal arrays, we don’t need to
mention the size when creating ArrayList. It
automatically adjusts its capacity as
elements are added or removed.

It may be slower than standard arrays, but it is helpful when the size is not
known in advance

Note that creating a large fixed-sized array would cause a waste of space since
ArrayList is part of the collection framework .

.it has better interoperability with other collections. For example, conversion
to a HashSet is straightforward.
Creating an ArrayList

Before using ArrayList, we need to import the java.util.ArrayList package first. Here is how we
can create arraylists in Java:

Here, Type indicates the type of an arraylist. For example,

In the above program, we have used Integer not int. It is because we cannot use primitive
types while creating an arraylist. Instead, we have to use the corresponding wrapper classes.
Here, Integer is the corresponding wrapper class of int.
Creating an ArrayList
1. Add Elements to an ArrayList

To add a single element to the arraylist, we use the add() method of the ArrayList class. For
example,
2. Remove ArrayList Elements

To remove an element from the arraylist, we can use the remove() method of the ArrayList
class. For example,
Converting an ArrayList to an Array

Following methods can be used for converting ArrayList to Array:


Using Object[] toArray() method

Syntax:

• It is specified by toArray in interface Collection and interface


List
• It overrides toArray in class AbstractCollection
• It returns an array containing all of the elements in this list
in the correct order
2: Using T[] toArray(T[] a

• Converts a list into an array


arr[] and returns same.
• If arr[] is not big enough, then
a new array of same type is
allocated for this purpose.
• T represents generic.
3: Manual method to convert ArrayList using get() method

We can use this method if we


don’t want to use java in built
toArray() method. This is a
manual method of copying all
the ArrayList elements to the
String Array[].

You might also like