黑马程序员_JavaAPI补充
2011-09-22 21:23
288 查看
---------------------- android培训、java培训、期待与您交流! -------------------------------------------- android培训、java培训、期待与您交流! ----------------------
1.vector和enumeration(两者都在java.util包中)
vector类是java语言提供的一种高级数据结构,可用于动态保存一系列对象。
对书上的实例结合高新技术中的自动装箱拆箱的技术后改写的程序,输入一串1位数后并相加。
import java.util.*;
class test
{
public static void main(String[] args)
{
Vector v1=new Vector();
while(true)
{
int ch=0;
try
{
ch=System.in.read();
}
catch(Exception e)
{e.printStackTrace();}
if (ch=='\r'|ch=='\n')
break;
else
v1.add(ch-'0');//将输入的字符转成数字并自动装箱成包装类加入向量
}
Enumeration e=v1.elements();
int sum=0;
while(e.hasMoreElements())
{
sum+=(Integer)e.nextElement();//自动拆箱
}
System.out.println(sum);
}
}
2.collection接口和iterator接口
按照java语法,colletion自己不能创建对象,要靠实现了它的arraylist来创建,iterator与enumeration的作用类似
在vector和Enumeration的程序的基础上略加修改后的程序:
import java.util.*;
class test
{
public static void main(String[] args)
{
ArrayList a1=new ArrayList();
while(true)
{
int ch=0;
try
{
ch=System.in.read();
}
catch(Exception e)
{e.printStackTrace();}
if (ch=='\r'|ch=='\n')
break;
else
a1.add(ch-'0');//将输入的字符转成数字并自动装箱成包装类加入向量
}
Iterator e=a1.iterator();
int sum=0;
while(e.hasNext())
{
sum+=(Integer)e.next();//自动拆箱
}
System.out.println(sum);
}
}
【Vector和Arraylist的区别】
Vector类中的所有方法都是线程同步的,两个线程并发访问Vector对象将是安全的,但即使只有一个线程访问,因为源程序仍然调用了同步方法,所以运行效率低些,Arraylist则刚好相反,没有线程同步,效率高些。
3。collection的子接口set和list(前者不能重复,后者有顺序)
查阅chm文档后,发现set和list不是一个一个对应的,比如说有arraylist但是没有arrayset,有enumset,但是没有enumlist。
实验打印效果和sort方法的程序:
import java.util.*;
class test
{
public static void main(String[] args)
{
ArrayList a1=new ArrayList();
while(true)
{
int ch=0;
try
{
ch=System.in.read();
}
catch(Exception e)
{e.printStackTrace();}
if (ch=='\r'|ch=='\n')
break;
else
a1.add(ch-'0');//将输入的字符转成数字并自动装箱成包装类加入向量
}
/*Iterator e=a1.iterator();
int sum=0;
while(e.hasNext())
{
sum+=(Integer)e.next();//自动拆箱
}
System.out.println(sum);*/
Collections.sort(a1);
System.out.println(a1);
}
}
输入:987654321
输出:[1,2,3,4,5,6,7,8,9]
【Hashtable与Properties类】
Hashtable也是一种高级数据结构,用以快速检索数据。它不仅像vector那样动态存储数据,而且还对每一存储的对象安排另一个对象(关键字)与之相关联。hashtable的key和value都不能为空对象null,如果新加的项目的key和以前有一样的,则覆盖原有的值。用作关键字的类必须覆盖object.hashcode和object.equals。
实例:
import java.util.*;
class testHashtable
{
public static void main(String[] args)
{
Hashtable h1=new Hashtable();
MyKey k1=new MyKey("e1",1);
MyKey k2=new MyKey("e2",2);
MyKey k3=new MyKey("e3",3);
h1.put(k1,1000);
h1.put(k2,2000);
h1.put(k3,3000);
Enumeration e=h1.keys();
while(e.hasMoreElements())
{
MyKey temp=(MyKey)e.nextElement();
System.out.println(h1.get(temp)+"yuan");
}
/*System.out.println(h1.get(k1)+"yuan");//如果用被注释的语句,结果是按照原来顺序打印的,而使用枚举的顺序刚好是相反的。
System.out.println(h1.get(k2)+"yuan");
System.out.println(h1.get(k3)+"yuan");*/
}
}
class MyKey//自定义的关键字,覆盖了equals和hashcode方法
{
int hao;
String name;
public MyKey(String name,int hao)
{
this.name=name;
this.hao=hao;
}
public boolean equals(MyKey key)
{
if (name.equals(key.name)&&(hao==key.hao))
return true;
else
return false;
}
public int hashCode()
{
return name.hashCode()+hao;
}
}
自定义的equals和hashcode方法是给hashtable的get方法来调用的
从hashtable中直接抓出来的key是不需要比较的,也就是不用调用上面两个方法,只有新建的对象才需要比较
---------------------- android培训、java培训、期待与您交流! -------------------------------------------- android培训、java培训、期待与您交流! ----------------------
1.vector和enumeration(两者都在java.util包中)
vector类是java语言提供的一种高级数据结构,可用于动态保存一系列对象。
对书上的实例结合高新技术中的自动装箱拆箱的技术后改写的程序,输入一串1位数后并相加。
import java.util.*;
class test
{
public static void main(String[] args)
{
Vector v1=new Vector();
while(true)
{
int ch=0;
try
{
ch=System.in.read();
}
catch(Exception e)
{e.printStackTrace();}
if (ch=='\r'|ch=='\n')
break;
else
v1.add(ch-'0');//将输入的字符转成数字并自动装箱成包装类加入向量
}
Enumeration e=v1.elements();
int sum=0;
while(e.hasMoreElements())
{
sum+=(Integer)e.nextElement();//自动拆箱
}
System.out.println(sum);
}
}
2.collection接口和iterator接口
按照java语法,colletion自己不能创建对象,要靠实现了它的arraylist来创建,iterator与enumeration的作用类似
在vector和Enumeration的程序的基础上略加修改后的程序:
import java.util.*;
class test
{
public static void main(String[] args)
{
ArrayList a1=new ArrayList();
while(true)
{
int ch=0;
try
{
ch=System.in.read();
}
catch(Exception e)
{e.printStackTrace();}
if (ch=='\r'|ch=='\n')
break;
else
a1.add(ch-'0');//将输入的字符转成数字并自动装箱成包装类加入向量
}
Iterator e=a1.iterator();
int sum=0;
while(e.hasNext())
{
sum+=(Integer)e.next();//自动拆箱
}
System.out.println(sum);
}
}
【Vector和Arraylist的区别】
Vector类中的所有方法都是线程同步的,两个线程并发访问Vector对象将是安全的,但即使只有一个线程访问,因为源程序仍然调用了同步方法,所以运行效率低些,Arraylist则刚好相反,没有线程同步,效率高些。
3。collection的子接口set和list(前者不能重复,后者有顺序)
查阅chm文档后,发现set和list不是一个一个对应的,比如说有arraylist但是没有arrayset,有enumset,但是没有enumlist。
实验打印效果和sort方法的程序:
import java.util.*;
class test
{
public static void main(String[] args)
{
ArrayList a1=new ArrayList();
while(true)
{
int ch=0;
try
{
ch=System.in.read();
}
catch(Exception e)
{e.printStackTrace();}
if (ch=='\r'|ch=='\n')
break;
else
a1.add(ch-'0');//将输入的字符转成数字并自动装箱成包装类加入向量
}
/*Iterator e=a1.iterator();
int sum=0;
while(e.hasNext())
{
sum+=(Integer)e.next();//自动拆箱
}
System.out.println(sum);*/
Collections.sort(a1);
System.out.println(a1);
}
}
输入:987654321
输出:[1,2,3,4,5,6,7,8,9]
【Hashtable与Properties类】
Hashtable也是一种高级数据结构,用以快速检索数据。它不仅像vector那样动态存储数据,而且还对每一存储的对象安排另一个对象(关键字)与之相关联。hashtable的key和value都不能为空对象null,如果新加的项目的key和以前有一样的,则覆盖原有的值。用作关键字的类必须覆盖object.hashcode和object.equals。
实例:
import java.util.*;
class testHashtable
{
public static void main(String[] args)
{
Hashtable h1=new Hashtable();
MyKey k1=new MyKey("e1",1);
MyKey k2=new MyKey("e2",2);
MyKey k3=new MyKey("e3",3);
h1.put(k1,1000);
h1.put(k2,2000);
h1.put(k3,3000);
Enumeration e=h1.keys();
while(e.hasMoreElements())
{
MyKey temp=(MyKey)e.nextElement();
System.out.println(h1.get(temp)+"yuan");
}
/*System.out.println(h1.get(k1)+"yuan");//如果用被注释的语句,结果是按照原来顺序打印的,而使用枚举的顺序刚好是相反的。
System.out.println(h1.get(k2)+"yuan");
System.out.println(h1.get(k3)+"yuan");*/
}
}
class MyKey//自定义的关键字,覆盖了equals和hashcode方法
{
int hao;
String name;
public MyKey(String name,int hao)
{
this.name=name;
this.hao=hao;
}
public boolean equals(MyKey key)
{
if (name.equals(key.name)&&(hao==key.hao))
return true;
else
return false;
}
public int hashCode()
{
return name.hashCode()+hao;
}
}
自定义的equals和hashcode方法是给hashtable的get方法来调用的
从hashtable中直接抓出来的key是不需要比较的,也就是不用调用上面两个方法,只有新建的对象才需要比较
---------------------- android培训、java培训、期待与您交流! -------------------------------------------- android培训、java培训、期待与您交流! ----------------------
相关文章推荐
- 黑马程序员_JAVA学习日记_JAVA中API中对象String类的补充
- 黑马程序员_JAVA学习日记_JAVA中API:集合框架1(Collection,List,Set及其子类和迭代器的应用)
- 黑马程序员_JAVA学习日记_JAVA中的多线程补充
- 黑马程序员---Java API
- 黑马程序员--javaAPI
- 黑马程序员-JavaAPI-16天-6(Map练习)
- 黑马程序员_JavaAPI——String及常见API类总结
- 黑马程序员——【Java基础】——泛型、Utilities工具类、其他对象API
- 黑马程序员-Java基础:常用API
- 黑马程序员_JAVA常用的类API(Scanner,String)
- 黑马程序员 —— JavaAPI_集合框架 (第十七天)
- 黑马程序员——Java基础---API常用类
- 黑马程序员Java培训、Android培训-Java 学习过程记录_JavaAPI-1
- 黑马程序员_Java API
- 黑马程序员-Java API-day15
- 黑马程序员-JavaAPI-17天-4(Arrays)
- 黑马程序员----------java基础API之String类的介绍和使用
- 腾讯 iweibo Java API 1.2.1 补充头像和个人信息更新功能接口
- 黑马程序员——12JavaAPI集合类
- 黑马程序员-----java基础 API学习