包装类 与 简单集合框架

一、包装类

1,Integer 类

构造方法:    Integer in1 = new(520); 

                      Integer in2 = new("520");

简单的运算结果

                      Interger in3 = 520;   //自动装箱。

                     in1.equals(in2)             // -------->    true   Interger类对equals方法进行了复写。比较两个对象里边的数值大小。

                     in1 == in2                      //----------> false   地址不同,所以为假    。

                     Integer c = 500;//自动装箱
                     Integer d = 500;//当对象值大于127即一个字节时 分配新地址
                     System.out.println(c == d);当 c、d都小于一个字节时 即(<=127)时,为true 否则 false。

基本的方法:

                         1,xxxxValue()                            以xxxx类型返回该Integer的数值。                                                       非静态。

                         2,parseInt(String s);                 以整数类型解析该字符串 。                                                                   静态

        其他类型到十进制

                         3,parseInt(String s, int radix)  使用第二个参数为指定的基数,解析该字符串,返回有符号的整数。   静态

                    示例: 
 / *
  *   parseInt("0", 10) 返回 0
  *   parseInt("473", 10) 返回 473
  *   parseInt("-0", 10) 返回 0
  *   parseInt("-FF", 16) 返回 -255
  *   parseInt("1100110", 2) 返回 102
  *   parseInt("2147483647", 10) 返回 2147483647
  *   parseInt("-2147483648", 10) 返回 -2147483648
  *   parseInt("2147483648", 10) 抛出 NumberFormatException
  *   parseInt("99", 8) 抛出 NumberFormatException
  *   parseInt("Kona", 10) 抛出 NumberFormatException
  *   parseInt("Kona", 27) 返回 411787

*/

十进制到其他类型:

                4,toxxxxString(int i);

                                    将整形数值 i 以 xxxx类型的字符串返回。xxxx包括 {"Binary","Octal","Hex" }二进制 八进制 十六进制。

5.  toString()                                                                   
 toString(int i)  返回一个表示指定整数的 String 对象。                     
 toString(int i,int radix)                                                    
                                                                                 
6. valueof(int i)   返回一个表示指定的 int 值的 Integer 实例。               
 valueof(String s)                                                            
 valueof(String s,int sradix)                                                  
                                                                                 

package com;

public class IntegerDemo {
public static void main(String[] args) {
		Integer i = new Integer("99");
		Integer i1 = new Integer(88);
		System.out.println(i + i1);

		// System.out.println(i+1);
		// System.out.println(i.toString());
		// System.out.println(Integer.parseInt("1234")); //输入字符串的正数类型,解析成十进制正数。
		// System.out.println(99+"  binary = "+Integer.toBinaryString(99));
		// System.out.println(Integer.toBinaryString(60)); 转换成二进制
		// System.out.println(Integer.toOctalString(60));转换成八进制
		// System.out.println(Integer.toHexString(60));转换成十六进制
		System.out.println(Integer.bitCount(3));

	}

}


 

二、集合类

      1,Collection 集合接口类 与 Iterator迭代器

                  Collection 为抽象接口类所以一般在创建对象时要用实现其功能的子类创建。

                           如:ArrayList    list  =  new ArrayList();

                                   Collection list = new ArrayLisy();

        集合常用方法:增加

                                           boolean   add(Object obj);添加元素

                                           boolean  add(Collection col);添加集合元素

                                  删除

                                            boolean  remove(Object obj);

                                            boolean  removeAll(Collection col);//删除该集合中和col共有的元素,即删除交集。

                                            boolean  retainAll(Collection  col);//删除该集合中不在col中的所有元素。

                                  判断

                                           boolean  contains(Object obj);

                                           boolean  contains (Collection col);

                                           boolean  isEmpty();

                                获取

                                            int size();//返回集合中的元素个数

                                             Object []  toArray();//返回包含此 collection 中所有元素的数组。

package Collection;

import java.util.ArrayList;
import java.util.Collection;

public class CollectionDemo {
public static void main(String[] args) {
		//创建集合类对象
		Collection col1 = new ArrayList();
		Collection col2 = new ArrayList();
		// show(col1);
		show(col1, col2);

	}

	public static void show(Collection col1, Collection col2) {
		col1.add("love01");
		col1.add("love02");
		col1.add("love03");
		col1.add("love04");
		col2.add("love02");
		col2.add("love03");
		// col2.add("love07");
		// col2.add("love08");
		System.out.println("col1 = " + col1);
		System.out.println("col2 = " + col2);
		// boolean b = col1.addAll(col2);
		// System.out.println("addAll "+b);
		// boolean b = col1.removeAll(col2);
		// System.out.println("removeAll "+b);
		System.out.print(col1.containsAll(col2));
		System.out.print(col1.contains("love04"));
		// System.out.println("col1 = " + col1);

	}

	public static void show(Collection col) {
		col.add("i");
		col.add("love");
		col.add("u");
		System.out.println(col);
		col.remove("u");
		System.out.println(col);
	}

}


 

Iterator的简单介绍

                   Iterator 迭代器类型的对象 需要用到集合类的 iterator()方法获得。同步不安全

                   如:    //创建集合对象

                                              Collection  col = new ArrayList();

                               //通过集合类获得迭代器对象

                             Iterator  it = col.iterator();  //   和在获取同步锁的监视器有类似。

//创建一个锁
 ReentrantLock lock = new ReentrantLock();
 //分别获取该锁上的两组监视器对象,一组用来监视生产者,一组用来监视消费者。
 Condition producer_con = lock.newCondition();
 Condition consumer_con = lock.newCondition();    

迭代器对象的只有三个方法即为:

  boolean hasNext();//是否仍有元素可以迭代。

 E  next();//返回下一个迭代的元素。

void remove()//从迭代器指向的 collection 中移除迭代器返回的最后一个元素(可选操作).   

package Collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class IteratorDemo {

	/**
	 * Iterator迭代器的方法演示
	 * 
	 */
	public static void main(String[] args) {
		//创建集合对象
		Collection col = new ArrayList();

		show(col);
	}

	public static void show(Collection col) {
//添加元素
		col.add("love01");
		col.add("love02");
		col.add("love03");
		col.add("love04");
		System.out.println(col);
		//调用Conllection的iterator方法,创建迭代器对象。
		
		Iterator it = col.iterator();
		
		while (it.hasNext())
			System.out.println(it.next());
		it.remove();
			System.out.println(col);

		/*
		 * System.out.println(it.next()); System.out.println(it.next());
		 * System.out.println(it.next()); System.out.println(it.next());
		 */

	}

}


  List集合接口 继承了Collection。仍为抽象接口

               特点: 有序的,元素带有索引(即含有角标)。

                                 主要方法:

  /**
  * List的主要方法
  * 1. 增
  *   boolean add(object);
  *   void  add(int index,object);
  *   boolean add(int index,Collection coll);
  *
  * 2. 删
  *   void clear();
  *   E remove(int index);//删除指定位置的元素,返回删除的元素。
  * 3. 修改
  *  E set(int index,object)//返回 被替换的元素。
  *4. 获取
  *  E get(int index)//返回指定位置的元素。
  *  int indexOf(obj)//返回该对象第一次出现的索引。如果没有,返回-1.
  *  int lastIndexOf(object)//返回该对象最后一次出现的索引。如果没有,返回-1.
  *  List  subList(fromIndex,toIndex)//返回从fromIndex(包含)到toIndex(不包含)之间的部分视图
  */

package List;

import java.util.ArrayList;
import java.util.List;

public class ListDemo {
public static void main(String[] args) {
		List li = new ArrayList();
		show(li);
		
	}

	public static void show(List li) {
		li.add("one");
		li.add("two");
		li.add("three");
		li.add("four");
		System.out.println(li);
		//增加元素
//		li.add(2, "hello");
		//删除元素
//		li.remove(2);
		//修改
//		System.out.println(li.set(3, "world!"));
		//获取
		System.out.println(li.get(2));
	}

}

ListIterator 迭代器类。

获取和Iterator 类似

先创建一个集合类对象。   

  ArrayList list = new ListArrayList();

获取该类的迭代器对象

ListIterator   listIte = list.listItertor();

允许程序员按任一方向遍历列表、迭代期间修改列表,并获得迭代器在列表中的当前位置,所以该迭代器对象是同步安全的。

如 如下代码:

public class ListDemo2 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//创建集合
		ArrayList li = new ArrayList();
		//根据list的iterator方法获取迭代器对象
		li.add("one");
		li.add("two");
		li.add("three");
		li.add(4);
		ListIterator list = li.listIterator();
		while(list.hasNext()){
			Object obj = list.next();
			if(obj.equals("two"))
					list.add("nine");
			else
				System.out.println(obj);			
		}
		System.out.println(li);
		
		/*Iterator it = li.iterator();
//		show(li);
		li.add("one");
		li.add("two");
		li.add("three");
		li.add(4);
		while(it.hasNext()){
			Object obj = it.next();
			if(obj.equals("two"))
				li.add("nine"); Iterator 同步问题导致抛出异常。解决办法用ListIterator的对象。
			else
				System.out.println(it.next());
			
		}*/

	}

	public static void show(ArrayList li) {
		li.add("one");
		li.add("two");
		li.add("three");
		li.add(4);
		Iterator it = li.iterator();
		while (it.hasNext()) {
			System.out.println("list : " + it.next());

		}
		for (int i = 0; i < li.size(); i++) {
			System.out.println(li.get(i));
		}
	}
}


你可能感兴趣的:(包装类 与 简单集合框架)