190 likes | 421 Views
Java Programming - Array & Examples -. 2014. Good Web Site for Java Students http://java.sun.com/docs/books/tutorial/java/index.html. Array. Array is Special object in Java Including the reference of data and some variables. Declaration. Declaring an array Not to create an array object
E N D
Java Programming- Array & Examples - 2014 Good Web Site for Java Students http://java.sun.com/docs/books/tutorial/java/index.html
Array • Array is Special object in Java • Including the reference of data and some variables
Declaration • Declaring an array • Not to create an array object • It creates a variable with a reference to an array • A variety of ways array declaration
Declaration I Type[] Name = {value1, …}; int[] a = {1, 3, 5, 2, 9, 6, 7}; String[] b = {“aaa”, “bbb”, “ccc”}; double[] c = {0.2, 0.001, 123.0};
Declaration I Type[] Name = {value1, …}; int[] a = {1, 3*2, 9-6, 7}; // a = [1, 6, 3, 7] a[0] = 8;// a = [8, 6, 3, 7] int[] b = a;// b = [8, 6, 3, 7]
Declaration II • Type[] Name = new Type[array_length]; • Automatically initialize with the default value • int[] a = new int[3]; // a = [0, 0, 0] • String[] b = new String[2+1]; // b = [null, null, null] • int c = 3; • double[] d = new double[c]; // c = [0.0, 0.0, 0.0]
Two-dimensional array • String[][] s = new String[10][10]; • String[][] s = new String[10][]; • for(int n = 0; n < 10; n++) s[n] = new String[n]; • Array length can be 0
Variable & Function • length • Return the array length • int[] a = {1, 2, 3}; • System.out.printfln(a.length); // 3 • toString() • Return a string representation of the object • int[] a = {1, 2, 3}; • System.out.printfln(a.toString()); // [I@188edd79 • equals() • Return true if two objects are equal • int[] a = {1, 2, 3}; • int[] b = {1, 2, 3}; • System.out.println(a.equals(b)); // false
Variable & Function • More useful functions are in the “arrays” class • Most methods are static • Import it by following declaration • Import java.util.Arrays;
Variable & Function • toString() • Return a string presentation of contents in array • int[] a = {1, 2, 3}; • System.out.printfln(Arrays.toString(a)); // [1, 2, 3] • equals() • Return true if contents of two arrays are equal • int[] a = {1, 2, 3}; • int[] b = {1, 2, 3}; • System.out.println(Arrays.equals(a, b)); // true • sort() • Return a sorted array • int[] a = {5, 9, 1}; • Arrays.equals(a); • System.out.println(Arrays.toString(a)); // [1, 5, 9]
Array-Like Data Structure • Must be import before coding • ArrayList • Import java.util.ArrayList; • HashMap • Import java.util.HashMap;
ArrayList • ArrayList is an Class • It is better to put the objects in the same format • All things in ArrayList will become “objects” • Not to declare the length of array • ArrayList<type> Name = new ArrayList<type>(); • ArrayList a = new ArrayList(); • ArrayList<int> a = new ArrayList<int>(); • ArrayList<ArrayList<int>> a = new ArrayList<ArrayList<int>();
import java.util.ArrayList; public class arraylist_example { public static void main(String args[]) { ArrayList a=new ArrayList(); ArrayList<String> b=new ArrayList<String>(); a.add(100); a.add("123"); a.add(456.99); System.out.println(a); a.remove(0); System.out.println(a); System.out.println(a.get(1)); System.out.println(a.indexOf("123")); System.out.println(a.indexOf(123)); b.addAll(a); System.out.println(b); System.out.println(b.indexOf("456.99")); System.out.println(b.size()); } }
HashMap • Data are stored as pairs • key : data • “a”:123 • “b”:”c++” • Find data with key only • Declaration • HashMap<key_type, data_type> Name = new HashMap<key_type, data_type >(); • HashMap<String, Integer> a = new HashMap<String, Integer >(); • HashMap<String, ArrayList> a = new HashMap<String, ArrayList> ();
import java.util.HashMap; import java.util.Iterator; public class hashmap_example { public static void main(String args[]) { HashMap<String,Integer> a=new HashMap<String,Integer>(); a.put("Java",100); a.put("C++",new Integer(2)); a.put("Teacher",new Integer(3)); a.put("Student",new Integer(3)); a.put(null,null); System.out.println(a); if(a.containsKey("Java")) System.out.println("This HashMap contains a key named Java."); System.out.println("The size of HashMap:"+a.size()); System.out.println("Remove C++"); a.remove("C++"); System.out.println("The size of HashMap:"+a.size()); Iterator ir=a.keySet().iterator(); while(ir.hasNext()) { String key_value=(String)ir.next(); System.out.println(key_value+":"+a.get(key_value)); } } }