Arrays Flashcards
what are Array?
A collection of variable of the same data type.
1. An Array in Java is an object
2. An have a fixed size.
3. An array variable referenced a group of data.
what null in Java?
The value of the object that reference noting.
string name: // null
how do you create Array?
- String[] name; // null
- String[] name = new String[4];
// create 4 array in the
memory
what are the default value give to Array when created.
- 0 for numeric data types
- null for reference types
- false for boolean types
4./u0000 for char types
How do you access Array elements?
use brackets and indices
Example:
int[] numbers = new int[4]
numbers[0] = 4;
// this assign 4 to the first element in the array
How do we print Array?
To print array there are two ways:
1.we use a for loop to print the array
2. we use a for each loop to print the array.
what are Anonymous Array?
An array without a variable referencing it.
new Scanner(System.in) .nextline();
How do we return array from method?
int[] numbers = getNumber();
printArray(numbers);
// we are returning an array form the method get numbers into the main method
public static in[] getNumber(){
return int[] {2,3,3,5,6};
}
How do pass Array to method
Array are passed by reference
Exceeding array bounds
The indices must be between 0 and length -1.
if you exceed the index of an array you will have an exception message printed to you on the console.
what is the Array class in java?
A class that contains some static methods that are used with arrays
what is the method used to sort array?
- this method sort the whole Array
The sort(); method is used to sort Array in Java.
example: Array.sort(string); - this method sort part of the array.
sort( Array, fromindex, to index);
what is the method use to search Array?
- this method is use to search
array: binarySearch();
example: binarySearch(numbers,4);
// we are searching for the value of 4 from an array created in the memory.
what is the method used to compare array?
we use the equal(); method to compare array in java
example:
Array.equal(number1, number2);
How do we fill Array with value?
we fill array using the fill method.
fill(array, value);
example: fill(number, 4)
fill(array, fromindex, to index, value);
How do we print Array?
- we print array using the toString() method in the array class.
example: Array.toString(numbers);
what is variable length Argument lists?
a variable number of arguments can be passed to a method.
example: sum(1,2,);
sum(1,3,8);
sum(1,2,3,4);
how do you pass a variable number of arguments to a method
What are 2D Array?
A two dimensional Array is a one dimensional array in which each element is another one dimensional array.
example: int[] [] numbers = new int[4];
what are ragged Arrays?
A 2D array with rows of different length.
{2,3,4}
{2,3,4,5}
{2,3,4,5,6}
In the 2D array the first dimension is what?
the first dimension is numbers of row.
how do we create a 2D Array?
int[][] numbers = new int[4][4];
int[][] numbers;
numbers[4][4];
Accessing 2D Array Element
the first row index is 0 and the second row index is one(1)
2. we can access the element using the index of row and column.
3. numbers[indexofrow][indexofcolumn]
How do we print 2D Array
- we can print 2D Array by accessing the element by it index and printing it.
example: numbers[0][0]
// this print the first element in the first row of the 2D arraya. print Row by Row
we can use a for loop to print row by row.
example: for( int i = 0; i < 3(amount of row); i++)
for(int j = 0; j < 4(amount of colum) j++);
System.out.println(number[i][j]) // print all the element in the array. i represent the row and j represent the column
b. column by Column
we also use a for loop for this
but i will be the column and j will be the row.
what are the other method to print the string representation of array?
deepToString(); method is use to print the string represenstation of an 2D array