Skip to content
Keyhan Hadjari edited this page Sep 12, 2016 · 6 revisions
  • Get size by: arr.length
  • CORRECT ARRAY initiation:
int [] numbers = new int[7]; // Get an int array with size 7;
int numbers2[] = new int[] {1, 33, 12, 55}; //Init array imediately
int numbers3 [] = {1, 33, 12, 55}; //This is correct too

java.util.Arrays.toString(numbers3); // This would print an array nicely
java.util.Arrays.sort(numbers3);// 1,12,33,55 sorts natuarally
//Before calling binarySearch, sort must be called other wise the search outcome
//would not be predictable.
java.util.Arrays.binarySearch(numbers3,33);// returns 2 since 33 is now third member
//If the value does no exist it would place it in correct place in array and show the index
//with a "-" for example 
java.util.Arrays.binarySearch(numbers3,4);// returns -1 

Multidimentional Arrays

int arr[][] = new int[5][5]; // int matrix with 5 rows and 5 columns
int []arr2[] = new int[5][5];// same as above
int[][] arr3 = new int[5][5];// same as above

int [][] asymetric = {{5,12,33},{7},{18,22},{68,44}}  // Creates Asymetric Multidimensional array.
int [][] asymetric2 = new int [2][]; // Also asymetric
asymetric2[0] = new int[3];
asymetric2[1] = new int[6];

//To access elements in a matrix
for(int i = 0; i < asymetric.length; ++i) {
    int[] arr = asymetric[i];
    for(int j = 0; i < arr.length; ++j) {
        System.out.println(j);
    }
}

//Easier access
for (int[] arr : asymetric) {
	for(int i: arr) {
	System.out.println(i);
	}
}

ArrayList

Since array size is set at initialization and cannot be changed, ther ArrayList is a way to give you dynamic array, where you could resize the arry just like a builder class add new members to it.

List<String> strArrList = new ArrayList<String>();
strArrList.add("Stockholm"); //ADD
strArrList.add(1,"Sweden"); //ADD
strArrList.remove("Sweden"); // removes first "Sweden" and returns true
strArrList.contains("Sweden"); //returns false since Stockholm has been removed
strArrList.remove(0); // returns "Stockholm" and removes it from array
strArrList.remove("HMM"); // returns false
strArrList.isEmpty(); //returns true since array is now empty
strArrList.size(); //returns 0 since no String in the array

List allArrList = new ArrayList(); //OK to mix in here
allArrList.add("Stockholm");
allArrList.add(11728);
allArrList.clear(); //empties whole ArrayList

List list1 = new ArrayList();
list1.add("a");
list1.add("b");
List list2 = new ArrayList();
list2.add("b");
list2.add("a");
list1.equals(list2); // Returns false since the order are different.

Object[] objcets = list1.toArray(); // Convert to array
String[] strings = strArrList.toArray(new String[0]); //convert to String array
String [] arr = {"Boo","Baa","DOO"};
List<String> list3 = Arrays.asList(arr); // This is not of type ArrayList class, but a static sized list.
list3.add("a"); // This would fail
list3.remove("Boo"); // This would fail
list3.set(1,"BAA"); // Is OK

List<String> list4 = Arrays.asList(5,2,3); // A simple way to create a list
Collections.sort(list4); //(2,3,5) sorts the List.

Clone this wiki locally