12月26日集合类
来源:互联网 发布:伊宁网站快速优化排名 编辑:程序博客网 时间:2024/04/29 00:50
======6B-1=====Integer===================================
八种数据类型都有对应的包装类
其中以Ineger为例
其中有一个构造函数Integer(String s); 可以接受一个将字符转来得到整数;
还有一个intValue()方法,返回值类型为Int,返回的是Integer对象中包装的整数值
静态方法。:parseInt(Spring s,int radix):返回整数。可以指定返回指定的进制的数 、
静态》valueOf(Sting s 返回类型为对象Integer的
利用字符串转换成对应的整数的三种办法:1.int w = new Integer(args[0]).intValue();
2.int h = new Integer.parseInt(args[1]);
3. int h = Integer.valueOf(args[1]).intValue();
代码:
public class First {
public static void main(String[] args) {
int w = new Integer(args[0]).intValue();
int h = Integer.parseInt(args[2]);
for(int i = 0 ;i< h;i++)
{
StringBuffer sb = new StringBuffer();
for(int j = 0; j<w;j++)
{
sb.append('*');
}
System.out.println(sb.toString());
}
}
}
===========6B-2========集合类=================================
java中的集合类:集合类是一组用于存储对象的类和接口;其中存储的每个对象诚挚为元素
经常用到的有:Vector Enumeration、ArrayList、Collection、Iterator、Set、List等
首先介绍Vector类和Enumeration接口:
Vector(类似于动态数组)
编程实例:将键盘上输入的一个数字序列中的每一位数字存储在Vector对象中,然后再屏幕上打印出每位数字想加的结果。例如,输入32,打印出5.输入1234,打印出10。
vector对象中有addElement();用于向其中增加元素(对象类型,可用Integer来包装)。
elements()方法返回实现Enumeration接口的实例对象。 Enumeration e = v.elements()。
Enumeration接口有nextElement()方法逐一取出Enumeration对象中保存的元素。(注:Enumeration对象内部有个指示器。指向下次调用所返回元素的位置)
当指示器指向空对象时。hasMoreElements()方法返回false,
==========示例程序==================================
public class First {
public static void main(String[] args) throws Exception{
Vector v = new Vector();
int b = 0;
System.out.println("please enter number:");
while(true)
{
try{
b = System.in.read();}
catch(Exception e){e.printStackTrace();}
if(b=='/r'||b=='/n')
break;
else
{
int num = b -'0';
v.addElement(new Integer(num));
}
}
int sum = 0 ;
Enumeration e = v.elements();
while(e.hasMoreElements())
{
Integer intObj = (Integer)e.nextElement();
sum+= intObj.intValue();
}
System.out.println(sum);
}
}
==12.26==Collection接口与Iterator接口=====
java2之后推出后,设计者设计的一个综合的数据结构。这些数据结构的基本接口是Collection接口。取出保存其中的对象,必须调用Collection接口的iterator()方法,返回一个Iterator接口。其功能与Enumeration接口十分相似。(设者不喜欢Enumeration接口内方法的长名字)。
利用实现接口的类创建对象;ArrayList类就是实现了Collection接口的类
编程实例:用ArrayList和Iterator改写上面的例子程序。
细节:ArrayList类中有类似Vector类中的addElement()方法,那就是add()方法。
public class First {
public static void main(String[] args) throws Exception{
ArrayList v = new ArrayList();
int b = 0;
System.out.println("please enter number:");
while(true)
{
try{
b = System.in.read();}
catch(Exception e){e.printStackTrace();}
if(b=='/r'||b=='/n')
break;
else
{
int num = b -'0';
v.add(new Integer(num));
}
}
int sum = 0 ;
Iterator e = v.iterator();
while(e.hasNext())
{
Integer intObj = (Integer)e.next();
sum+= intObj.intValue();
}
System.out.println(sum);
---------由上可见Vector类和ArryList类都可以实现,但何时用ArrayList何时用Vector呢-----
Vector类中的所有方法都是线程同步的,如果用两个线程并发的去访问Vector对象,那这是线程安全的。(但即使只有一个线程的话,Vector对象还是会用线程监视器检查的情,这就需要系统额外的开销,使系统的利用率降低)
ArrayList类中的所有方法是不同步的,如果程序中不存在使用多线程的安全问题是,使用ArrayList的运行效率比Vector高些,但如果使用ArrayList类,并且存在多线程的安全问题,则需要自己编写代码对访问的对象进行同步处理,使用Vector则不用。
===========================Collection、Set、List的区别如下=====================================
Collection是Set和List的父类,是通用性的集合接口。
Collection中各对象元素是没有指定顺序的,可以存在重复的元素和多个null的元素。(无法实现排序)
Set中的个元素中没有指定顺序,但其中不允许有重复元素,最多允许有一个null对象
List类中各个元素对象之间是指定的顺序的,允许有重复元素和多个null对象元素。(可以排序)====Arraylist类是实现了List接口的类(Collection下面是List,List下面是ArrayList)
下面对ArrayList对象中的元素排序:
public class First {
public static void main(String[] args) throws Exception{
ArrayList al = new ArrayList();
al.add(new Integer(1));
al.add(new Integer(3));
al.add(new Integer(2));
System.out.println(al.toString());
Collections.sort(al);//Collections类中的成员方法都是静态的如sort()方法,主要是用于操作集合对象,本身并不是集合类对象,而是提供各种方法去操作集合类对象。
System.out.println(al.toString());
}
}
- 12月26日集合类
- 12月26日
- 6月24日集合框架
- 2007年12月26日
- 2009年12月26日
- 12月26日iframe执行
- 2012年12月26日 工作总结
- 2014年12月26日
- 2014年12月26日
- 2014年12月26日
- 2月26日
- 8月26日。
- 2月26日
- 3月26日
- 10月26日
- 3月26日
- 7月26日
- 6月26日
- 帝王为何被称为“九五之尊”
- 美国总统喜欢引用中国古诗词
- 民间流传光绪死因的8个版本
- 关于数据存储的一些琐事
- 读《程序员羊皮卷》有感
- 12月26日集合类
- 大四当这样
- 作为网络管理员到底应该掌握什么知识?
- struts2配置的action没反应的问题
- 成年人必看的5个故事
- 操作系统的第一个作业。。。要求界面的我还不会弄。主体
- 开发人员一定要加入收藏夹的网站
- 一个自定义转换器的模板。
- 新来