当前位置: 技术问答>java相关
请给个使用Hashtable和List的例子。
来源: 互联网 发布时间:2015-03-05
本文导语: Hashtable和List应该是比较常用的类吧? Java刚上手,不好意思,请教各位前辈了。 | hashtable 的一例: import java.util.*; import java.io.*; public class HashtableD{ public static void main(String[] args){ ...
Hashtable和List应该是比较常用的类吧?
Java刚上手,不好意思,请教各位前辈了。
Java刚上手,不好意思,请教各位前辈了。
|
hashtable 的一例:
import java.util.*;
import java.io.*;
public class HashtableD{
public static void main(String[] args){
Hashtable numbers= new Hashtable();
numbers.put("one", new String("ppp"));
numbers.put("two", new Integer(2));
numbers.put("three", new Integer(3));
Integer n = (Integer) numbers.get("two");
String temp=(String) numbers.get("one");
if(n!=null)
System.out.println("two="+n);
System.out.println("one="+temp);
}
linkedlist 的例子:
import java.util.*;
import java.io.*;
public class ListDemo{
public static void main(String[] args){
int i=2;
LinkedList linklist = new LinkedList();
linklist.add(new String("first"));
linklist.add(new Integer("1"));
linklist.add(new String("third"));
linklist.add(i,new String("second"));
String firstr=(String)linklist.getFirst();
String secstr=(String)linklist.get(i);
linklist.remove(2);
String rsec=(String)linklist.get(i);
System.out.println("first="+firstr);
System.out.println("before remove seconc="+secstr);
System.out.println("after remove second="+rsec);
}
}
上面的例子虽然简单, 但却也实现的hash表和list 的一些基本功能,
}
import java.util.*;
import java.io.*;
public class HashtableD{
public static void main(String[] args){
Hashtable numbers= new Hashtable();
numbers.put("one", new String("ppp"));
numbers.put("two", new Integer(2));
numbers.put("three", new Integer(3));
Integer n = (Integer) numbers.get("two");
String temp=(String) numbers.get("one");
if(n!=null)
System.out.println("two="+n);
System.out.println("one="+temp);
}
linkedlist 的例子:
import java.util.*;
import java.io.*;
public class ListDemo{
public static void main(String[] args){
int i=2;
LinkedList linklist = new LinkedList();
linklist.add(new String("first"));
linklist.add(new Integer("1"));
linklist.add(new String("third"));
linklist.add(i,new String("second"));
String firstr=(String)linklist.getFirst();
String secstr=(String)linklist.get(i);
linklist.remove(2);
String rsec=(String)linklist.get(i);
System.out.println("first="+firstr);
System.out.println("before remove seconc="+secstr);
System.out.println("after remove second="+rsec);
}
}
上面的例子虽然简单, 但却也实现的hash表和list 的一些基本功能,
}
|
/**
* @version 1.00 1999-07-07
* @author Cay Horstmann
*/
import java.util.*;
public class MapTest
{ public static void main(String[] args)
{ Map staff = new HashMap();
staff.put("144-25-5464", new Employee("Angela Hung"));
staff.put("567-24-2546", new Employee("Harry Hacker"));
staff.put("157-62-7935", new Employee("Gary Cooper"));
staff.put("456-62-5527", new Employee("Francesca Cruz"));
// print all entries
System.out.println(staff);
// remove an entry
staff.remove("567-24-2546");
// replace an entry
staff.put("456-62-5527", new Employee("Francesca Miller"));
// look up a value
System.out.println(staff.get("157-62-7935"));
// iterate through all entries
Set entries = staff.entrySet();
Iterator iter = entries.iterator();
while (iter.hasNext())
{ Map.Entry entry = (Map.Entry)iter.next();
Object key = entry.getKey();
Object value = entry.getValue();
System.out.println("key=" + key + ", value=" + value);
}
}
}
class Employee
{ public Employee(String n)
{ name = n;
salary = 0;
}
public String toString()
{ return "[name=" + name + ", salary=" + salary + "]";
}
public void setSalary(double s)
{ salary = s;
}
private String name;
private double salary;
}
* @version 1.00 1999-07-07
* @author Cay Horstmann
*/
import java.util.*;
public class MapTest
{ public static void main(String[] args)
{ Map staff = new HashMap();
staff.put("144-25-5464", new Employee("Angela Hung"));
staff.put("567-24-2546", new Employee("Harry Hacker"));
staff.put("157-62-7935", new Employee("Gary Cooper"));
staff.put("456-62-5527", new Employee("Francesca Cruz"));
// print all entries
System.out.println(staff);
// remove an entry
staff.remove("567-24-2546");
// replace an entry
staff.put("456-62-5527", new Employee("Francesca Miller"));
// look up a value
System.out.println(staff.get("157-62-7935"));
// iterate through all entries
Set entries = staff.entrySet();
Iterator iter = entries.iterator();
while (iter.hasNext())
{ Map.Entry entry = (Map.Entry)iter.next();
Object key = entry.getKey();
Object value = entry.getValue();
System.out.println("key=" + key + ", value=" + value);
}
}
}
class Employee
{ public Employee(String n)
{ name = n;
salary = 0;
}
public String toString()
{ return "[name=" + name + ", salary=" + salary + "]";
}
public void setSalary(double s)
{ salary = s;
}
private String name;
private double salary;
}