Code
package test;
import java.util.*;
final class Apple {
private String str = "apple";
public Apple(String str) {
this.str = str;
}
public String getStr() {
return this.str;
}
}
public class Hi {
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
System.out.println("hello");
// ArrayList的基本用法
ArrayList apples = new ArrayList();
ArrayList<Apple> appleList = new ArrayList<Apple>();
for (int i = 0; i < 3; i++) {
apples.add(new Apple("arrayList"));
appleList.add(new Apple("arrayList"));
}
for (int i = 0; i < apples.size(); i++) {
String str = ((Apple) apples.get(i)).getStr();
System.out.println(str);
}
for (Apple c : appleList) {
System.out.println(c.getStr());
}
// HashMap的基本用法
HashMap hApples = new HashMap();
HashMap<String, Apple> hAppleList = new HashMap<String, Apple>();
for (int i = 0; i < 3; i++) {
hApples.put(Integer.toString(i), new Apple("hashMap"));
hAppleList.put(Integer.toString(i), new Apple("hashMap"));
}
for (int i = 0; i < hApples.size(); i++) {
String str = ((Apple) hApples.get(Integer.toString(i))).getStr();
System.out.println(str);
}
// 遍历key
for (Iterator iter = hAppleList.keySet().iterator(); iter.hasNext();) {
String str = (String) iter.next();
System.out.println(str);
}
for (Apple c : hAppleList.values()) {// 遍历value
String str = (String) c.getStr();
System.out.println(str);
}
// Iterator
ListIterator listIterator = apples.listIterator();
for(;listIterator.hasNext();){
((Apple)listIterator.next()).getStr();
}
}
}
package test;
import java.util.*;
final class Apple {
private String str = "apple";
public Apple(String str) {
this.str = str;
}
public String getStr() {
return this.str;
}
}
public class Hi {
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
System.out.println("hello");
// ArrayList的基本用法
ArrayList apples = new ArrayList();
ArrayList<Apple> appleList = new ArrayList<Apple>();
for (int i = 0; i < 3; i++) {
apples.add(new Apple("arrayList"));
appleList.add(new Apple("arrayList"));
}
for (int i = 0; i < apples.size(); i++) {
String str = ((Apple) apples.get(i)).getStr();
System.out.println(str);
}
for (Apple c : appleList) {
System.out.println(c.getStr());
}
// HashMap的基本用法
HashMap hApples = new HashMap();
HashMap<String, Apple> hAppleList = new HashMap<String, Apple>();
for (int i = 0; i < 3; i++) {
hApples.put(Integer.toString(i), new Apple("hashMap"));
hAppleList.put(Integer.toString(i), new Apple("hashMap"));
}
for (int i = 0; i < hApples.size(); i++) {
String str = ((Apple) hApples.get(Integer.toString(i))).getStr();
System.out.println(str);
}
// 遍历key
for (Iterator iter = hAppleList.keySet().iterator(); iter.hasNext();) {
String str = (String) iter.next();
System.out.println(str);
}
for (Apple c : hAppleList.values()) {// 遍历value
String str = (String) c.getStr();
System.out.println(str);
}
// Iterator
ListIterator listIterator = apples.listIterator();
for(;listIterator.hasNext();){
((Apple)listIterator.next()).getStr();
}
}
}