1 / 15

Java Programming - Array & Examples -

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

haven
Download Presentation

Java Programming - Array & Examples -

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Programming- Array & Examples - 2014 Good Web Site for Java Students http://java.sun.com/docs/books/tutorial/java/index.html

  2. Array • Array is Special object in Java • Including the reference of data and some variables

  3. 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

  4. 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};

  5. 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]

  6. 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]

  7. 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

  8. 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

  9. Variable & Function • More useful functions are in the “arrays” class • Most methods are static • Import it by following declaration • Import java.util.Arrays;

  10. 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]

  11. Array-Like Data Structure • Must be import before coding • ArrayList • Import java.util.ArrayList; • HashMap • Import java.util.HashMap;

  12. 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>();

  13. 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()); } }

  14. 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> ();

  15. 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)); } } }

More Related