0% found this document useful (0 votes)
66 views3 pages

Finals Comprog Reviewer

This document discusses one-dimensional and multi-dimensional arrays in Java. It defines arrays as collections of multiple values of the same data type. One-dimensional arrays can be initialized during declaration with specific values. Multi-dimensional arrays organize elements into rows and columns like a grid. The Array class contains useful methods for manipulating arrays, such as filling arrays with a value, sorting array elements, and sorting a range of elements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views3 pages

Finals Comprog Reviewer

This document discusses one-dimensional and multi-dimensional arrays in Java. It defines arrays as collections of multiple values of the same data type. One-dimensional arrays can be initialized during declaration with specific values. Multi-dimensional arrays organize elements into rows and columns like a grid. The Array class contains useful methods for manipulating arrays, such as filling arrays with a value, sorting array elements, and sorting a range of elements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

ONE-DIMENSIONAL ARRAY }

/* Each element in the "prices" array is


sequentially assigned to the variable
Arrays 'x', and then 'x' is printed. The loop
continues until all elements in the
- collection of multiple values, all of the "prices" array have been processed.*/
same data type.
- have a specific length set at creation,
and each element is accessed using a TWO-DIMENSIONAL ARRAY
numerical index starting from 0.
2D Array
Declaration (general syntax):
- like a grid, organizing elements in rows
dataType arrayName[] = new and columns, all of the same data type
dataType[size]; OR dataType arrayName[]
Declaring (general syntax):
= {elements};
dataType[][] arrayName = new
Declaration:
dataType[rowSize][columnSize];
int nums[] = new int[5];
//array name is "nums" int[][] scores = new int[4][5];
//nums array stores 5 values, those Col 1 Col 2 Col 3 Col 4 Col 5
values are called elements.
Elements – values inside the group. can be Row [0][0] [0][1] [0][2] [0][3] [0][4] 1

accessed using indices: num[0], num[1], Row [1][0] [1][1] [1][2] [1][3] [1][4] 2
num[2], num[3], num[4] Row [2][0] [2][1] [2][2] [2][3] [2][4] 3
Note: every value in programming starts at index “0”. Row [3][0] [3][1] [3][2] [3][3] [3][4] 4

- can also be initialized with specific Initialization during declaration - involves


values during declaration: enclosing each row's elements in braces,
double prices[] = {12.25, 32.50, 16.90, separated by commas, and enclosing all rows
1.25, 43}; in outer braces:
Examples: int[][] board = {
{2, 3, 1},
//Declaration {15, 25, 13},
int ageList[] = new int[5]; {20, 4, 7},
//Initialization - process of assigning {11, 18, 14}
a value to the Variable };
ageList[0] = 18; Bali yung row niyan is yung inner curly braces. Kung
ageList[1] = 19; ilang pair yung inner curly braces niyo, yun yung count
ageList[2] = 20; ng row niyo. (white kulay nung inner braces niyo, then
ageList[3] = 30; green yung outer braces)
ageList[4] = 41;
//Accesses and display 3 Ways to process an array:
System.out.println(ageList[0]);//prints
18 1. Entire array – This means working with
//Initialized with specific values every element in the two-dimensional
during declaration array.
double prices[] = {12.25, 32.50, 16.90,
1.25, 43};
//Can perform math inside the square
bracket
int x = 3;
System.out.println(prices[x +
1]);//returns 43.0
To access all the elements inside of an array,
we can use for loop and/or for each loop.
// FOR EACH LOOP:
for (double x : prices) {
System.out.println(x);
2. Row processing - doing something
with just one entire row of numbers.

Yung first square bracket sa


declaration is these inner braces.
int[][][] threeD = new
int[2][3][4];
3. Column processing - doing
something with just one entire
column of numbers.
So parang group
siya, 2 groups. Since nag start lagi sa ‘0’
yung index, meron tayong group 1 index 0
and group 2
index 1.
MULTIDIMENSIONAL ARRAY
Inside the group braces, meron
type of array where each element can itself
tayong rows.
be an array. Basically, instead of having a
single-dimensional array (a list of
elements), you have arrays of arrays.
Declaration (general syntax):
dataType[][]...[] arrayName = new
dataType[exp1][exp2]...[n]; Then inside the rows, meron tayong columns, also
dataType: This is the type of data that the elements of known as elements. Pero yung naka paloob sa outer
the array will hold. braces (Yellow arrows), is also called elements.

arrayName: This is the name you give to your array


variable. Yung ginawa natin:

exp1, exp2, ..., n: These are expressions representing threeD[0][0][0] = 9;


the size of each dimension of the array. It specifies
yung binigay natin ng value is yung sa group
how many elements are there along each dimension.
1 row 1 column 1.
Example 3D ARRAY:
COMBINING 2D ARRAYS TO MAKE 3D
int[][][] threeD = new int[2][3][4]; ARRAY:
This is a 3D array with 2 layers, each layer int[][] odd = {
having 3 rows and 4 columns. {1, 3, 5, 7, 9},
{11, 13, 15, 17, 19},
Example: {21, 23, 25, 27, 29}
};
int[][][] threeD = new int[2][3][4]; int[][] even = {
threeD[0][0][0] = 9; {2, 4, 6, 8, 10},
System.out.println(threeD[0][0][0]); {12, 14, 16, 18, 20},
//output 9 {22, 24, 26, 28, 30}
Bali yung magiging itsura niya sa table niyan };
//combine these arrays into a 3D array.
is: In this case, numbers is a 3D array
containing both odd and even arrays:
int[][][] numbers = {odd, even};
Now, numbers has two "groups" (because
it's 3D), and each group contains a 2D
array (odd or even).
Accessing Information about the 3D Array:
System.out.println(numbers.length);
//This prints the number of groups in Arrays.fill(arrayName, type val) - Sets
numbers, which is 2. In this case, it's
the number of 2D arrays (odd and even). every element in an array to a specified
value.
System.out.println(numbers[0].length);
//This prints the number of lists (rows) Arrays.fill(a, 8);
in the first group (which is odd). It for(int x : a) {
returns 3 because odd has 3 rows. System.out.print(x);
}// changes all values of array 'a' to 8.
System.out.println(numbers[0] Arrays.sort(arrayName) - Arranges the
[2].length); elements in an array in ascending order.
//This prints the number of elements in
the third list (row) of the first group Arrays.sort(b);
(odd). It returns 5 because the third for(int x : b) {
row in odd has 5 elements. System.out.print(x);
}// sorts the elements of array 'b'.
Arrays.sort(arrayName, int fromIndex, int
THE ARRAY CLASS toIndex) - (with specific range): Sorts only a
- Methods for performing array specific range of elements in an array.
manipulations in the Arrays class from Arrays.sort(b, 1, 5);// sorts only a
the java.util package. specific range of elements in array 'b'
(from index 1 to 4).
- These methods are overloaded for all
primitive types. (Overload - PRE-DEFINED FUNCTIONS
overloading refers to a concept where
multiple functions or methods in a
program can have the same name but
differ in terms of the type or number of
their parameters).
- Methods/functions inside Arrays class
are static method, meaning they are
used with the class name without
instantiating an Arrays object.
int[] a = {1, 3, 4, 5, 7, 4};
int[] b = {6, 7, 12, 5, 1, 2};
int[] c = {1, 3, 4, 5, 7, 4};
Arrays.binarySearch(arrayName, type key)
- Searches for a specific value in an array
using a fast algorithm. It returns the index if
found, otherwise, it returns a negative value
that represents where the value could be
inserted to maintain the sorted order.
int x = Arrays.binarySearch(a, 4);
System.out.println(x);// returns 2
because 4 is found at index 2 in array
'a'.

Arrays.equals(arrayName, arayName) -
Checks if two arrays of the same type are
equal (dapat equal yung elements). Returns
true if they are, otherwise, false.
boolean x = Arrays.equals(a, c);
System.out.println(x);// returns true
because arrays 'a' and 'c' are equal.

You might also like