java笔记三

java 代码
  1. /*java迭代器Iterator  
  2.  * 1)使用方法iterator()要求容器返回一个Iterator。第一次调用Iterator的  
  3.  * next()方法,它返回序列的第一个元素。  
  4.  * 2)使用next()方法获得序列的下一个元素。  
  5.  * 3)使用hasNext()方法检查序列中是否还有元素  
  6.  * 4)使用remove()将迭代器新近返回的元素删除  
  7. */  
  8. package c11;   
  9. import java.util.*;   
  10.   
  11. class Cat {   
  12.    private int catNumber;   
  13.    public Cat(int i) { catNumber = i; }   
  14.    public void id() {   
  15.      System.out.println("Cat #" + catNumber);   
  16.    }   
  17.  }   
  18. public class CatsAndDogs2 {   
  19.  public static void main(String[] args) {   
  20.   List cats = new ArrayList();   
  21.   for(int i = 0; i<7 ; i++)   
  22.    cats.add(new Cat(i));   
  23.   Iterator e = cats.iterator();   
  24.   while(e.hasNext())   
  25.    ((Cat)e.next()).id();   
  26.      
  27.  }   
  28. }   
  29.   


下面是一个通用打印的例子

java 代码
  1. //: c11:Printer.java   
  2. // Using an Iterator.   
  3. // From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002   
  4. // www.BruceEckel.com. See copyright notice in CopyRight.txt.   
  5. import java.util.*;   
  6.   
  7. public class Printer {   
  8.   static void printAll(Iterator e) {   
  9.     while(e.hasNext())   
  10.       System.out.println(e.next());   
  11.   }   
  12. ///:~   
  13.   

 

在java中不允许程序员重载操作符
String类对象是一个常量对象

 

java 代码
  1. //针对八种基本数据类型定义的相应的引用类型-----封装类   
  2. public class Test {   
  3.  public static void main(String[] args) {   
  4.   int i=3;   
  5.   Integer in = new Integer(i);//构造一个新分配的 Integer 对象,它表示指定的 int 值   
  6.   int j=in.intValue();//以 int 类型返回该 Integer 的值   
  7.   System.out.println("j="+j);   
  8.   String str=in.toString();//返回一个表示该 Integer 值的 String 对象   
  9.   System.out.println("str="+str);   
  10.   String str1="123";   
  11.   System.out.println(Integer.valueOf(str1));//返回保持指定的 String 的值的 Integer 对象。   
  12.      
  13.   //更多方法参看java  API帮助文档docs...   
  14.  }   
  15.   
  16. }   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值