double[] price = new double[10];界定一含10個倍準浮點數的陣列。
price[4] = 29.95; System.out.println("The price of this item is $" + price[4]);
例題: BarGraph.java
Vector staffVector = new Vector(); String more; do { String name = readString("Name: "); double salary = readDouble("Salary: "); Employee e = new Employee(name, salary); staffVector.addElement(e); }
// print out the whole vector data int i; for (i=0; i < staffVector.size(); i++) { Employee e = (Employee) staffVector.elementAt(i); System.out.println(e.getName() + " " + e.getSalary()); } // another way to print for (Enumeration e = staffVector.elements(); e.hasMoreElements(); ) { System.out.println(e.nextElement().toString()); //? }
staffVector.setElementAt(boss, 0); int n = staffVector.size();注意: 求陣列、向量、和字串的長度,用法不同
Array a.length Vector a.size() String a.length()
// converting Vectors to Arrays Employee[] staff = new Employee[staffVector.size()]; staffVector.copyInto(staff);
例題: Staff.java
例題: Table2.java