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

9f Arrays in Java (1)

The document provides an overview of arrays in Java, explaining their structure, declaration, initialization, and methods. It highlights the limitations of fixed-size arrays and introduces ArrayLists for dynamic sizing. Additionally, it covers multi-dimensional arrays, array operations using the Arrays class, and the Java Random class for generating pseudorandom numbers.

Uploaded by

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

9f Arrays in Java (1)

The document provides an overview of arrays in Java, explaining their structure, declaration, initialization, and methods. It highlights the limitations of fixed-size arrays and introduces ArrayLists for dynamic sizing. Additionally, it covers multi-dimensional arrays, array operations using the Arrays class, and the Java Random class for generating pseudorandom numbers.

Uploaded by

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

Object Oriented

Paradigm

Arrays

1
Arrays
Introduction to Arrays in Java
• Arrays store multiple values of the same data type in a single variable.
• Fixed-size, indexed collection of elements.
• Can be one-dimensional or multi-dimensional.
• Improves code efficiency by avoiding multiple variables.
Declaration and Initialization
Arrays
Multi-Dimensional Arrays
Arrays
Array Methods in Java
• Arrays.toString(): Converts array to a string.
• Arrays.equals(arr1, arr2): Compares two arrays.
• Arrays.fill(arr, value): Fills array with a value.
• Arrays.fill(numbers, 0); // Sets all elements to 0
Limitations of Arrays & Dynamic Size
Arrays
• Fixed size once declared.
• No built-in dynamic resizing.
• Use ArrayList for dynamic size.

ARRAYLIST FOR DYNAMIC SIZE


ArrayList<Integer> list = new ArrayList<>();
list.add(10);
list.add(20);
Java Arrays
• Arrays are an important structure to hold data.
• Java allows us to hold many objects of the same type
using arrays. OR Arrays in Java are homogeneous data
structures implemented in Java as objects.
• It can be used with the help of a loop to access the
elements by their index.
Two-step Process
• First, you must declare a variable of the desired array
type
• Second, you must allocate the memory that will hold
the array, using new, and assign it to the array variable
int[] age;
age = new int[10];

OR

int[] age = new int[10];


Java Arrays
Arrays -
Example For Loop

class ArrayExample { class ArrayExample {


public static void main(String[] args) { public static void main(String[]
args) {

// create an array of length 5


int[] age = new int[5];
int[] age = new int[5];

// accessing elements using


// access each array element by index
for loop
number
for (int i = 0; i < 5; ++i)
System.out.println(age[0]);
{
System.out.println(age[1]);
System.out.println(age[2]); System.out.println(age[i]);
System.out.println(age[3]); }
System.out.println(age[4]); }
} }
}
JAVA Multi-dimensional Arrays
Arrays -
Example
class Mul2D{
public static void main(String args[]) {
int mul2d[][]= new int[4][5];
int i, j, k = 0;
for(i=0; i<4; i++)
for(j=0; j<5; j++) {
Mul2D[i][j] = k; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
k++;
}
for(i=0; i<4; i++) {
for(j=0; j<5; j++);
System.out.print(mul2d[i][j] + " ");
System.out.println();
}
}
}
Multidimensional arrays
Parsing Array to a
Method
public class PMethods{
public static void display(int y[])
{
System.out.println(y[0]); 1
System.out.println(y[1]);
System.out.println(y[2]); 2

3
}
public static void main(String args[])
{
int x[] = { 1, 2, 3 };
display(x);
}
}
Array Length
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
System.out.println(cars.length);
// Outputs 4

Loop Through an Array


String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
for (int i = 0; i < cars.length; i++) {
System.out.println(cars[i]);
}
Loop Through an Array with For-
Each
for (type variable : arrayname) {
...
}

String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};


for (String i : cars) {
System.out.println(i);
}

It can be read like this: for each String element (called i - as


in index) in cars, print out the value of i.
Array Class
• import java.util.Arrays import java.util.Arrays;

• Has static methods to operate public class Main {


on arrays public static void main(String[]
args)
• The syntax of this class: {
Arrays.<function name>; // Get the Array
int intArr[] = { 10, 20, 15, 22, 35
};
Arrays class provides several static
methods that can be used to perform Arrays.sort(intArr);
these tasks directly without the use of int intKey = 22;
loops. System.out.println(intKey+ " found
• Fill an array with a particular value. at index = “ + Arrays.binarySearch(intArr,
• Sort an Arrays. intKey));
• Search in an Arrays.
}
• And many more.
}
Methods() Description
static int
This method uses a binary search algorithm to search
binarySearch(elementToBeSearche
the specified element in the array.
d)
compare(array 1, array 2) It compares two arrays passed as parameters.
It compares two arrays, numerically treating elements
compareUnsigned(array 1, array 2)
as unsigned.
static boolean deepEquals(Object[] It returns true if the two specified arrays are deeply
a, Object[] b) equal to one another
equals(array1, array2) It checks if both the arrays are equal or not

fill(originalArray, fillValue) It assigns this fillValue to each index of this Array

hashCode(originalArray) It returns an integer hashCode of the specified array.


It searches and returns the index of the first
mismatch(array1, array2)
unmatched element between the two specified arrays.
static List asList(T… a) It returns a fixed-size list backed by the specified Array

It copies the specified array, truncating the default


copyOf(originalArray, newLength)
Example
import java.util.Arrays;
public class Main {
public static void main(String[] args)
{
// Get the Array
int intArr[] = { 10, 20, 15, 22, 35 };
// Get the second Array
int intArr1[] = { 10, 15, 22 };
// To compare both arrays
System.out.println("Integer Arrays on comparison: "
+ Arrays.compare(intArr, intArr1));
}
}
Java Random doubles() Method
public DoubleStream doubles()
public DoubleStream doubles(double randomNumberOrigin, double
randomNumberBound)
public DoubleStream doubles(long streamSize)
public DoubleStream doubles(long streamSize, double randomNumberOrigin,
double randomNumberBound)

streamSize- the number of values to generate

randomNumberOrigin- the origin of each random


value

randomNumberBound- the bound of each random


value
Java Random Class
• Java Random class is used to generate a stream
of pseudorandom numbers.

Methods( Description
)
doubles() Returns an unlimited stream of pseudorandom double
values.
ints() Returns an unlimited stream of pseudorandom int
values.
longs() Returns an unlimited stream of pseudorandom long
values.
next() Generates the next pseudorandom number.
Methods() Description
nextByte() Generates random bytes and puts them into a specified byte
array.
nextDouble( Returns the next pseudorandom Double value between 0.0
) and 1.0 from the random number generator's sequence
nextFloat() Returns the next uniformly distributed pseudorandom Float
value between 0.0 and 1.0 from this random number
generator's sequence
nextGaussia Returns the next pseudorandom Gaussian double value with
n() mean 0.0 and standard deviation 1.0 from this random
number generator's sequence.
nextInt() Returns a uniformly distributed pseudorandom int value
generated from this random number generator's sequence
nextLong() Returns the next uniformly distributed pseudorandom long
value from the random number generator's sequence.
Longs value : java.util.stream.LongPipeline$Head@14ae5a5

Example Random boolean value : true


Random bytes = ( 57 77 8 67 -122 -71 -79 -62 53 19 )

byte[] bytes = new byte[10];


import java.util.Random;
//generates random bytes
public class JavaRandomExample1 { and put them in an array
public static void main(String[] args) random.nextBytes(bytes);
{
System.out.print("Random
//create random object bytes = ( ");
Random random= new Random(); for(int i = 0; i<
//returns unlimited stream of bytes.length; i++)
pseudorandom long values {
System.out.println("Longs System.out.printf("%d
value : "+random.longs()); ", bytes[i]);
// Returns the next pseudorandom }
boolean value
System.out.print(")");
boolean val =
random.nextBoolean(); }
System.out.println("Random boolean }
value : "+val);
Example
import java.util.Random; //value after setting seed
public class JavaRandomExample2 { System.out.println("Seed
value : "+random.nextInt());
public static void main(String[] args)
{ //return the next
pseudorandom long value
Random random = new Random();
Long val = random.nextLong();
//return the next pseudorandom
integer value
System.out.println("Random
System.out.println("Random Integer
Long value : "+val);
value : "+random.nextInt());
}
// setting seed
}
long seed =20;
random.setSeed(seed); Random Integer value : 1294094433
Seed value : -1150867590
Random Long value : -7322354119883315205
THANK YOU….

You might also like