JAVA作业
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
3. 假设一个学生所选课程为语文、数学、英语、政治、物理、化学,给出此学生所选课程的Vector列表,并访问物理存放在Vector中的位置。
import java.util.Vector;
public class VECTOR {
public static void main(String[] args){
Vector vec=new Vector();
vec.add("语文") ;
vec.add("数学") ;vec.add("英语") ;vec.add("政治") ;vec.add("化学") ;
vec.add(4,"物理");
System.out.print(vec.get(4) + " ");
}
}
4、统计一个字符数组里每个字符出现的次数,统计一个字符串里每个字符出现的次数。[使用Hashtable实现]
import java.util.Hashtable;
import java.util.Iterator;
public class UseHashtable {
public Hashtable table;
public int i;
UseHashtable()
{
t able=new Hashtable
}
public void hashInsert(String s){
if (table.containsKey(s)){
i=(Integer)table.get(s);
table.put(s,++i);
}
else{
table.put(s, 1);
}
}
public Hashtable getHashtable(){
return table;
}
public static void main(String[] args) {
UseHashtable a=new UseHashtable();
a.hashInsert("ab");
a.hashInsert("bc");
a.hashInsert("ab");
a.hashInsert("ab");
a.hashInsert("bc");
a.hashInsert("bcd");
a.hashInsert("c");
Hashtable table=a.getHashtable();
while(it.hasNext()){
temp=(String)it.next();
System.out.println(temp+"出现了"+table.get(temp)+"次");
}
}
}
5. 对于图3.12中的Shebei表,将设备编码和设备名称作为Hashtable中的键和值进行存储,然后访问此Hashtable,找到键0008和0016并输出设备编码为0008、0016的设备名称。
import java.util.Hashtable;
public class UseHashtable {
public static void main(String[] args)
{
Hashtable ht=new Hashtable();
ht.put("0001", "打印机");
ht.put("0002", "扫描机");
ht.put("0003", "闹钟");
ht.put("0005", "文具盒");
ht.put("0006", "钢笔");
ht.put("0007", "圆珠笔");
ht.put("0008", "书桌");
ht.put("0009", "起重机");
ht.put("0010", "椅子");
ht.put("0012", "收音机");
ht.put("0013", "电脑");
ht.put("0014", "钟表");
ht.put("0015", "雨伞");
ht.put("0016", "自行车");
System.out.println("设备编码:0008 设备名称:"+ht.get("0008"));
System.out.println("设备编码: 0016 设备名称:"+ht.get("0016"));
}
}