一瞥 Java Arrays
Java Arrays
An array is a data structure that stores a collection of values of the same type. You access each individual value through an integer index.
For example, if a is an array of integers, a[i] is the ith integer in the array.
1 Declare and Initalize
int[] a; // specify the array type.
int[] a = new int[100]; // declare and initalize one array of 100 integers.
Once you have created an array, you can fill the elements in an array.
int[] a = new int[100];
for(int i = 0; i < a.length; i++) // you can find the number of elements of an array use array.length
a[i] = i; // fills this array with numbers from 0 to 99
Note
The defalut initialization value is 0 for int[], false for boolean[] and null for String[].
int[] small Primes = {2, 3, 5, 7}; //shortcut for creating an array
smallPrimes = new int[] {2,3,5,7};
2 The “for each” Loop
This relieve you from fussing with index values.
for (variable : collection) statement;
// here is an example which print each element of an array
int[] a = new int[100];
for (int element: a)
System.out.println(element);
Tip:
If you just want to see what’s in an array real quick, use Arrays.toString() method.
System.out.println(Arrays.toString(a));
3 Array Copying
You can copy one array variable into another, but then both variables refer to the same array.
int[] arrOne = arrTwo;
arrOne[3] = 12; // now arrTwo[3] is also 12
If you have ever learnt C, you will find this familiar. Pointers, right?
Now if you really want to copy all values of an array into a new one, use copyOf method in the Array class:
int[] copiedArr = Arrays.copyOf(arr, arr.length);
// the second parameter is the length of the new array, a common use would be
newArray = Arrays.copyOf(arr, 2 * arr.length);
// or you can copy a range of one array to another
newArray = Arrays.copyOfRange(type[] a, int start, int end)
4 Command-Line Parameters
You have seen in your first line of java code, String[] args
public class Message{
public static void main(String[] args){
System.out.println("hello world!");
}
}
actually the main method receives an array of strings specified on the commond line.
->> java Message -h world
then args[0] would be “-h”.
So far, this is not that useful to us.
5 Array Sorting
To sort an array of numbers, use the sort method in the Arrays class.
int[] a = new int[100];
...
Arrays.sort(a);
其他 java.util.Arrays , API
1. binarySearch
search for the value v, if it is found, return its index, otherwise, negative value r is returned. -r-1, (aka, -(r+1)) is the spot at which v should be inserted to keep array sorted.
static int binarySearch(type[] a, type v)
static int binarySearch(type[] a, int start, int end, type v)
/*
a: a sorted array of type int, long short, char, byte, float, or double
start: The starting index (inclusive)
end: The ending index (exclusive)
v: A value of the same type as the elements of a.
*/
2. fill
Sets all elements of the array to v.
static void fill(type[] a, type v)
a: an array of type int, long short, char, byte, float, or double
v: a value of the same type as a
3. equals
Returns true if the arrays have the same length and if the elements in corresponding index match.
static boolean equals(type[] a, type[] b)
a, b: arrays of type int, long short, char, byte, float, or double
6 Multidimensional Arrays
double[][] balances; //an example
balances = new double[12][100]
// if you new the array elements, shorthand notation
int[][] magicSquare =
{
{16,3,2,13},
{5,10,11,18},
{9,6,7,12},
[4,15,14,1}
};
// if you want to use for-each loops
for (double[] row: a)
for(double value: row)
dosomething with value;
// or you can just use for loop inside another for loop
Tip
To pring our a quick and dirty look of elements in a two-dementional array
System.out.println(Arrays.deepToString(a));
7 One more thing about Arrays
Well in fact, java has no multidimensional arrays at all, only one-dimensional arrays. Multidimensional arrays are faked as “arrays of arrays.”
Since rows of arrays are individually accessible, you can actually swap them
double[] temp = balances[i];
balances[i] = balances[i+1];
balances[i+1] = temp;
Finale
So far that’s the basics of what you need to know, now let’s go and Ace those leetcode problems under Array tag.