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);