??xml version="1.0" encoding="utf-8" standalone="yes"?> import java.net.*; class WithoutFinally q段代码创徏了一个套接字Qƈ调用 accept Ҏ(gu)。在退Ҏ(gu)之前Q?zhn)必须关闭此套接字Q以避免资源漏洞。ؓ了完成这一dQ我们在 //2 处调?closeQ它是该Ҏ(gu)的最后一条语句。但是,如果 try 块中发生一个异怼怎么样呢Q在q种情况下,//2 处的 close 调用永远不会发生。因此,(zhn)必L莯个异常,q在重新发出q个异常之前?//1 处插入对 close 的另一个调用。这样就可以保在退Ҏ(gu)之前关闭套接字?/p>
q样~写代码既麻烦又易于出错Q但在没?finally 的情况下q是必不可少的。不q的是,在没?finally 机制的语a中,E序员就可能忘记以这U方式组l他们的代码Q从而导致资源漏z。Java 中的 finally 子句解决了这个问题。有?finallyQ前面的代码可以重写ؓ以下的Ş式: import java.net.*; class WithFinally finally 块确?close Ҏ(gu)总被执行Q而不?try 块内是否发出异常。因此,可以保在退Ҏ(gu)之前M调用 close Ҏ(gu)。这h可以确信套接字被关闭ƈ且?zhn)没有泄漏资源。在此方法中不需要再有一?catch 块。在W一个示例中提供 catch 块只是ؓ了关闭套接字Q现在这是通过 finally 关闭的。如果?zhn)实提供了一?catch 块,?finally 块中的代码在 catch 块完成以后执行?/p>
finally 块必M try ?try/catch 块配合用。此外,不可能退?try 块而不执行?finally 块。如?finally 块存在,则它M执行。(无论从那点看Q这个陈q都是正的。有一U方法可以退?try 块而不执行 finally 块。如果代码在 try 内部执行一?System.exit(0); 语句Q则应用E序l止而不会执?finally 执行。另一斚wQ如果?zhn)?try 块执行期间拨掉电(sh)源,finally 也不会执行。) 作者:陈健?发文旉Q?004.03.10 ׃本文旨在探讨Java"异常机制"的深层原理,因此关于"异常"的用方法都不做详细说明。首先看一D非常熟(zhn)的用于打开一个文件的CE序D: FILE *fp; fp=fopen(filename,"rw"); if(fp==NULL){ printf("cannot open file\n"); exit(0); } 在这D늨序中Qif条g语句中的一D는来处理没有找到指定文Ӟ或者其它原因无法正打开指定文g。可是如果遇C个责d不强的程序员Q他可能认ؓ出现找不到文件的可能性很,或者由于思\集中在程序功能的实现上而忘C处理q种情况。这时程序同样可以正编译,而且一般情况下也不会出现问题。但此时q段E序可以肯定说是不够健壮的,而且一旦这D늨序发生了错误也会让程序员很难发现错误出在哪里。在C语言以及其它大多数高U语a中都可以丑և很多q种例子?/p>
也就是一个函数在使用的时候,可能会出现ƈ没有辑ֈq个函数的用目的的情况Q哪怕在q段E序的特定用环境下发生q种异常情况的可能性只有万分之一。常用处理的Ҏ(gu)是Q程序员在需要用某个函数时必须充分了解可能会有什么原因导致该函数不能正确执行Q然后加入相应的条g判断语句来进行处理。后面将有一个例子说明这个问题?/p>
而Java?异常机制"是在处理上q问题中l了E序员非常简单而灵zȝ方式。一般来_其它高语言主要是让函数使用者来x该函数可能会出现的异常情况,而java则是把这件事情交l方?和函数对应的概念Q在Java中称Ҏ(gu))的设计者来做。这对于Ҏ(gu)的用者来说带来的方便是不会因d不强Q或者办事丢三那四,会忘了在使用Ҏ(gu)时处理可能发生的异常情况。而麻烦就是,在用一个可能会发生异常的方法时Q绝对不能视而不见,而必d出相应的处理。也是说象上述CE序D中Q如果忘了ifE序块,q个E序甚至q能蒙过一个外行上司,但当使用Java来完成这个功能时Q只要用到的Ҏ(gu)使用?异常"机制Q如果不对可能?异常"的方法进行相应处理,java~译器是不会让其通过的?/p>
一?异常c?的组lŞ?/strong> JavapȝcM的方法生的异常都被l织?异常c?Q还有Errorc,不在本文讨论范围Q,此方法和它相关的"异常c?通过throws关键字关联在一Pq且q些c都必须是Exceptioncȝ子类。Q何一个自己开发的cȝҎ(gu)中如果可能会产生某种异常Q也可以这U异常组l成一?异常c?Q但q个"异常c?同样必须是Exception的子c,或孙子类{等?/p>
?Q?/p>
/*isLegal于检查数据是否合法,?gt;0时视为合法,q回合法| *否则视ؓ不合法,抛出"异常"?/ int isLegal(int dt) throws LowZeroException//q种定义本文中均UCؓҎ(gu)?异常"?/p>
{ //qthrows建立了关?/p>
if(dt>=0){ return data; } else throw new LowZeroException(); } /*自已写的异常c,l承自Exception*/ class LowZeroException extends Exception { public LowZeroException(){ super(); } } 仔细观察Ҏ(gu)isLegal()Q它体现出的最值得注意的特色是Q它有两U方式的函数出口Q一U是通过return语句Q返回的是方法本w定义的cd的实例,另一U是通过throwQ返回的?异常c?的对象实例,Java中称之ؓ抛出"异常"。对比一下C中如何处理同L问题的: int isLegal(int dt) { if(dt>=0){ return data; } else return -1;//通过一个特定值来表明出错 } ׃C只能通过returnq回函数|所以在处理异常情况时则可能通过以上方式来处理。当然这p求isLegal()函数的用者必ȝ道函C使用q回?1来表明出C合法数据的情c?/p>
Ҏ(gu)q两U处理方法,可以知道java?异常机制"把处理异怺件的职能和方法本w的职能通过两个不同出口分离开来?/p>
所有这?异常c?独立于它具体服务的方法被l一l织成一个类树?异常机制"好比高校的后勤C会化一P通过后勤C会化将学校的教学职能和学校的后勤保障分d来,q且后勤集团的组lŞ式也是独立于学校M的。事实证明,q种l织方式不仅提高了服务效率,也提高了服务质量。整个Java体系中的"异常c?l织形式如图1所C: 在例1中的isLegal()Ҏ(gu)如果在调用过E中没有能正常返回整形数Q而是?异常"产生点生了"异常"对象Q那么这?异常"对象p来接Ӟq处理它呢?以下来解答q个问题?/p>
二?异常"的处理过E?/strong> Java中由try…catch语法来处?异常"Q将兌?异常c?的方法包含在try{}E序块中Qcatch(){}关键字可以用Ş参,用于和方法生的"异常"对象l合。当调用某个Ҏ(gu)Ӟ引v异常事g发生的条件成立,便会抛出"异常"Q原来的E序程会在此Ҏ(gu)处中断,然后try模块后紧跟的catch中的"形参"和此异常对象完成了结合,l而进入了catch模块中运行。具体过ED例说明: ?Q?/p>
/*关联有异常的方法包含在try模块?/ int myMethod(int dt){ int data = 0; try{ int data = isLegal(dt); }catch(LowZeroException e){ System.out.println("发生数据错误Q?); } return data; } 三?异常"的处理方?/strong> 有两U方法处?异常"Q第一U如?Q将含有"异常"出口的方法直接放到try块中Q然后由紧随其后的catch块捕捉。第二种是不直接监听捕捉被引用方法的"异常"Q而是这?异常"兌传递给引用Ҏ(gu)Q同时监听捕捉工作也相应向上传递?/p>
?Q?/p>
int myMethod2(int dt) { int data = 0; try{ data = myMethod(dt) }catch(LowZeroException e){ System.out.println("发生数据错误Q?); e.printStackTrace(); } return data; } int myMethod(int dt) throws LowZeroException { int data = isLegal(dt); //此处引用isLegal()Ҏ(gu)Q但q没有捕捉它?异常" return data; } 从上例中可以看到Ҏ(gu)myMethod()与它引用的方法isLegal()产生?异常"LowZeroException建立了关联,也就是完成了?异常"兌的向上传递,此时的myMethod()Ҏ(gu)体中虽然只有一个returnq回语句Q但它事实上同样有两U方式的函数出口Q一U是由returnq回的整形|另一U则是返回方法名中的throws关键字所指的"异常c?的实例对象。相应的Q监听捕捉的工作交给了上一层方法myMethod2()。同L道理QmyMethod2()也可以将"异常"通过throws的关联l向上传递。这L话,一旦一?异常"被捕捉到Ӟq个"异常"必有一个传递\径,而如果我们在捕捉点的catchE序块中加入printStackTrace()Ҏ(gu)Q便能清楚的看到q个"异常"是怎样传递过来的。例如在?如果?异常"被捕捉到Qe.printStackTrace()打印出来的结果将是: LowZeroException: at Example.isLegal at Example myMethod at Example.myMethod2 at Example main 从上l果中我们可以看刎ͼ从LowZeroException"异常"产生点,卛_含throw new LowZeroException();子句的方法开始,然后一直追溯到产生当前U程的方法(注意QprintStackTrace()q不是追溯到捕捉点结束,而是C生当前线E的Ҏ(gu)l束Q?异常"产生点生的LowZeroException"异常"对象Q首先被赋给了isLegal()兌的LowZeroExceptioncȝ无名引用Q然后l赋lmyMethod()兌的LowZeroExceptioncȝ无名引用Q再l箋赋给myMethod2()中的catch块中的Ş参eQ最后在q里被处理掉Q这?异常"对象随即消失。可以说Qcatch(){}是"异常"对象的生命终l点?/p>
另外q要注意一点,Ҏ(gu)?异常"的关联可以一直向上传递,当传递到与mainҎ(gu)兌后,卛_main()Ҏ(gu)的定义中使用了throws ExceptionQ这旉了虚拟机没有其它Ҏ(gu)能够引用main()Ҏ(gu)Q且在程序中可能看不到try…catchE序块,但ƈ不会产生错误Q因为此时虚拟机会捕?异常"Qƈ且会默认的调用printStackTrace()Ҏ(gu)打印?异常"路径。M只要一个方法关联了"异常"Q可以将q个"异常"兌向上传递,但是最l必M用catch来终?异常"Q或者一直传递到main()Ҏ(gu)交给Java虚拟机来l束"异常"对象的生命,否则是通不q编译的?/p>
四、?异常机制"的需要注意的几点 1.一个方法中可能会生多U不同的异常Q你可以讄多个"异常"抛出Ҏ(gu)解决q个问题?/p>
2."异常"对象从生点产生后,到被捕捉后终止生命的全过E中Q实际上是一个传DE,所以你可以Ҏ(gu)需要,来合理的控制到"异常"的粒度。例如在?中,如果你ƈ不需要知道具体生的是LowZeroException"异常"Q那么你可以使用"异常"的公qcException来结?异常"对象Q即catch(Exception e){…}。同样在"异常"与方法关联的传递过E中Q也可以Ҏ(gu)需要控制关?异常"的粒度,即throws后面跟上异常对象的父cd?/p>
3."异常机制"中还有一U特D情况――RuntimeException"异常c?Q见?Q,q个"异常c?和它的所有子c都有一个特性,是"异常"对象一产生pJava虚拟机直接处理掉Q即在方法中出现throw 子句的地方便被虚拟机捕捉了。因此凡是抛U?q行时异?的方法在被引用时Q不需要有try…catch语句来处?异常"?/p>
怕万一q篇文章链接失效Q收藏v来。Be Careful With Transient Data
l于明白了?br />
当串行化某个对象Ӟ如果该对象的某个变量是transientQ那么这个变量不会被串行化进厅R也是_假设某个cȝ成员变量是transientQ那么当通过ObjectOutputStream把这个类的某个实例保存到盘上时Q实际上transient变量的值是不会保存的。因为当从磁盘中dq个对象的时候,对象的该变量会没有被赋倹{?br />
另外q篇文章q提刎ͼ当从盘中读出某个类的实例时Q实际上q不会执行这个类的构造函敎ͼ而是dq个cȝ实例的状态,q且把这个状态付l这个类的对象。这Ҏ(gu)以前g不知道?br />
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1064847
Be Careful With Transient Data
(原文来自http://www.devx.com/tips/Tip/13726)
Expertise: Intermediate
Language: Java
January 28, 2000
Be Careful With Transient Data
Java's serialization provides an elegant, and easy to use mechanism for making an object's state persistent. While controlling object serialization, we might have a particular object data member that we do not want the serialization mechanism to save.
To turn off serialization on a certain field of an object, we tag that field of the class of our object with the Java's "transient" keyword. This, to low-level parts of the Java virtual machine, is an indication that the transient variable is not part of the persistent state of an object.
First, let's have some backgrounder code with Java's serialization.
Suppose we define a class as:
public class LoggingInfo implements java.io.Serializable
{
private Date loggingDate = new Date();
private String uid;
private transient String pwd;
LoggingInfo(String user, String password)
{
uid = user;
pwd = password;
}
public String toString()
{
String password=null;
if(pwd == null)
{
password = "NOT SET";
}
else
{
password = pwd;
}
return "logon info: "n " + "user: " + uid +
""n logging date : " + loggingDate.toString() +
""n password: " + password;
}
}
Now we can create an instance of this class and serialize it, and write the serialized object to disk as in:
LoggingInfo logInfo = new LoggingInfo("MIKE", "MECHANICS");
System.out.println(logInfo.toString());
try
{
ObjectOutputStream o = new ObjectOutputStream(
new FileOutputStream("logInfo.out"));
o.writeObject(logInfo);
o.close();
}
catch(Exception e) {//deal with exception}
To read the object back, we can write
try
{
ObjectInputStream in =new ObjectInputStream(
new FileInputStream("logInfo.out"));
LoggingInfo logInfo = (LoggingInfo)in.readObject();
System.out.println(logInfo.toString());
}
catch(Exception e) {//deal with exception}
If we run this code, we notice that the read-back object prints password as "NOT SET". This is exactly the effect we should have expected when we declared the pwd field as transient.
Now, let's see a potential problem that careless treatment of transient fields may cause. Suppose we modify our class definition and provide default values for the transient field, say we write:
public class GuestLoggingInfo implements java.io.Serializable
{
private Date loggingDate = new Date();
private String uid;
private transient String pwd;
GuestLoggingInfo()
{
uid = "guest";
pwd = "guest";
}
public String toString()
{
//same as above
}
}
Now, if we serialize an instance of GuestLoggingInfo, write it to disk, and read it back, we still see that the read-back object prints password as "NOT SET". In effect, the process of reading back (de-serializing) totally ignores the constructor of GuestLoggingInfo. So what happened?
The answer lies in the fact that the initialization code is not called because we are not initializing, in other words, we are not constructing a brand new object, but loading back the persistent state of an object of a class, and assigning that state to another object of the same class. Declaring the pwd field as transient, excludes the data for that
]]>
import java.io.*;
{
public void foo() throws IOException
{
//在Q一个空闲的端口上创Z个套接字
ServerSocket ss = new ServerSocket(0);
try
{
Socket socket = ss.accept();
//此处的其他代?..
}
catch (IOException e)
{
ss.close(); //1
throw e;
}
//...
ss.close(); //2
}
}
import java.io.*;
{
public void foo2() throws IOException
{
//在Q一个空闲的端口上创Z个套接字
ServerSocket ss = new ServerSocket(0);
try
{
Socket socket = ss.accept();
//此处的其他代?..
}
finally
{
ss.close();
}
}
}
]]>
import java.text.DateFormat;
/**
* 格式化时间类
* DateFormat.FULL = 0
* DateFormat.DEFAULT = 2
* DateFormat.LONG = 1
* DateFormat.MEDIUM = 2
* DateFormat.SHORT = 3
* @author Michael
* @version 1.0Q?2007/03/09
*/
public class Test{
public static void main(String []args){
Date d = new Date();
String s;
/** Datecȝ格式: Sat Apr 16 13:17:29 CST 2006 */
System.out.println(d);
System.out.println("******************************************");
/** getDateInstance() */
/** 输出格式: 2006-4-16 */
s = DateFormat.getDateInstance().format(d);
System.out.println(s);
/** 输出格式: 2006-4-16 */
s = DateFormat.getDateInstance(DateFormat.DEFAULT).format(d);
System.out.println(s);
/** 输出格式: 2006q??6?星期?*/
s = DateFormat.getDateInstance(DateFormat.FULL).format(d);
System.out.println(s);
/** 输出格式: 2006-4-16 */
s = DateFormat.getDateInstance(DateFormat.MEDIUM).format(d);
System.out.println(s);
/** 输出格式: 06-4-16 */
s = DateFormat.getDateInstance(DateFormat.SHORT).format(d);
System.out.println(s);
/** 输出格式: 2006-01-01 00:00:00 */
java.text.DateFormat format1 = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
s = format1.format(new Date());
System.out.println(s);
/** 输出格式: 2006-01-01 00:00:00 */
System.out.println((new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss")).format(new Date()));
/** 输出格式: 20060101000000***/
java.text.DateFormat format2 = new java.text.SimpleDateFormat("yyyyMMddhhmmss");
s = format2.format(new Date());
System.out.println(s);
}
}
行数据的输入、输出操作,Java中把不同的输入、输出源Q键盘、文件、网l连接等Q抽象ؓ“?#8221;QStreamQ,有两U基本的:输入和输出?/p>
Q?Q输入流Q只能从中读取数据,而不能向其写出数据?/p>
Q?Q输出流Q只能向其写出数据,而不能从中读取数据?/p>
实际上指在计算机的输入与输Z间运动的数据的序列,序列中的数据既可以是未l加工的原始二进制数据,也可以是l一定编码处理后W合某种格式规定的特定数据,如字W流序列、数字流序列{?/p>
1.1、字节流与字W流
按处理数据的cdQ流可以分ؓ字节和字符,它们处理的信息的基本单位分别是字节(byteQ与字符QcharQ?/p>
输入字节的cMؓInputStream
输出字节的cMؓOutputStream
输入字符的cMؓReader
输出字符的cMؓWriter
如下表,q四个类是抽象类Q其他的输入输出类都是它们的子cR?/p>
字节与字符?/p>
|
字节?/p> |
字符?/p> |
输入 |
InputStream |
Reader |
输出 |
OutputStream |
Writer |
Ҏ(gu)摘要 | |
---|---|
int |
available() q回此输入流Ҏ(gu)的下一个调用方可以不受dC此输入流dQ或跌Q的字节数?/td> |
void |
close() 关闭此输入流q放与该流兌的所有系l资源?/td> |
void |
mark(int readlimit) 在此输入中标记当前的位|?/td> |
boolean |
markSupported() 试此输入流是否支持 mark ?reset Ҏ(gu)?/td>
|
abstract int |
read() 从输入流d下一个数据字节?/td> |
int |
read(byte[] b) 从输入流中读取一定数量的字节q将其存储在~冲区数l? b 中,以整数Ş式返回实际读取的字节数?/td>
|
int |
read(byte[] b, int off, int len) 输入流中最? len 个数据字节读入字节数l,读取的W一个字节存储在元素 b[off] 中,下一个存储在 b[off+1] 中,依次cL。读取的字节数最多等?len ?/td>
|
void |
reset() 此重新定位到Ҏ(gu)输入最后调? mark Ҏ(gu)时的位置?/td>
|
long |
skip(long n) 跌和放弃此输入中? n 个数据字节?/td>
|
Ҏ(gu)摘要 | |
---|---|
void |
close() 关闭此输出流q放与此流有关的所有系l资源?/td> |
void |
flush() h此输出流q强制写出所有缓冲的输出字节?/td> |
void |
write(byte[] b) ? b.length 个字节从指定的字节数l写入此输出?/td>
|
void |
write(byte[] b, int off, int len) 指定字节数l中从偏U量 off 开始的 len 个字节写入此输出?/td>
|
abstract void |
write(int b) 指定的字节写入此输出流?/td> |
abstract void
close()
void
mark(int readAheadLimit)
boolean
markSupported()
int
read()
int
read(char[] cbuf)
abstract int
read(char[] cbuf, int off, int len)
int
read( boolean
ready()
void
reset()
long
skip(long n)
Ҏ(gu)摘要 | |||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| |||||||||||||||||
append(CharSequence csq, int start, int end) 指定字W序列的子序列追加到?writer.Appendable?/td> | |||||||||||||||||
abstract void |
close() 关闭此流Q但要先h它?/td> | ||||||||||||||||
abstract void |
flush() h此流?/td> | ||||||||||||||||
void |
write(char[] cbuf) 写入字符数组?/td> | ||||||||||||||||
abstract void |
write(char[] cbuf, int off, int len) 写入字符数组的某一部分?/td> | ||||||||||||||||
void |
write(int c) 写入单个字符?/td> | ||||||||||||||||
void |
write( |
write(
|
处理cd | 字节?/td> | 字符?/td> |
Buffering~冲 |
BufferedInputStream BufferedOutputStream |
BufferedReader BufferedWriter |
Filteringqo |
FilterInputStream FilterOutputStream |
FilterReader FilterWirter |
Converting between bytes and character 字节{为字W流 |
InputStreamReader OutputStreamWriter |
|
Object Serialization 对象序列?/p> |
ObjectInputStream ObjectOutputStream |
|
Date coversion 基本数据cd转化 |
DateInputStream DateOutputStream |
|
Counting行号处理 |
LineNumberInputStream | LineNumberReader |
Peeking ahead可回退?/p> |
PushBackInputStream | PushbackReader |
Pinting可显C处?/td> | PrintStream | PrintWriter |
除了Collection外,集合中还有Comparator、Iterator、ListIterator和RandomAccess接口?br />Comparator定义怎样比较两个对象QIterator和ListIterator列D集合中的对象Q而通过实现RandomAccessQ列表允许对其元素进行有效的随机讉K?br />
Collection接口Q?/strong>
Collection接口是构造集合框架的基础Qƈ声明了各U集合所共有的核心方法。JDK 不提供此接口的Q?i>直接 实现Q它提供更具体的子接口(?Set ?ListQ实现?br />Collection接口中的Ҏ(gu)Q?br />
Method Summary | ||
---|---|---|
boolean
|
add
(E o)
Ensures that this collection contains the specified element (optional operation). |
|
boolean
|
addAll
(Collection<? extends E> c)
Adds all of the elements in the specified collection to this collection (optional operation). |
|
void
|
clear
()
Removes all of the elements from this collection (optional operation). |
|
boolean
|
contains
(Object o)
Returns true if this collection contains the specified element. |
|
boolean
|
containsAll
(Collection<?> c)
Returns true if this collection contains all of the elements in the specified collection. |
|
boolean
|
equals
(Object o)
Compares the specified object with this collection for equality. |
|
int
|
hashCode
()
Returns the hash code value for this collection. |
|
boolean
|
isEmpty
()
Returns true if this collection contains no elements. |
|
Iterator<E>
|
iterator
()
Returns an iterator over the elements in this collection. |
|
boolean
|
remove
(Object o)
Removes a single instance of the specified element from this collection, if it is present (optional operation). |
|
boolean
|
removeAll
(Collection<?> c)
Removes all this collection's elements that are also contained in the specified collection (optional operation). |
|
boolean
|
retainAll
(Collection<?> c)
Retains only the elements in this collection that are contained in the specified collection (optional operation). |
|
int
|
size
()
Returns the number of elements in this collection. |
|
Object[]
|
toArray
()
Returns an array containing all of the elements in this collection. |
|
|
toArray
(T[] a)
Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array. |
Method Summary | ||
---|---|---|
boolean | add(E o) Appends the specified element to the end of this list (optional operation). | |
void | add(int index, E element) Inserts the specified element at the specified position in this list (optional operation). | |
boolean | addAll(Collection<? extends E> c) Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator (optional operation). | |
boolean | addAll(int index, Collection<? extends E> c) Inserts all of the elements in the specified collection into this list at the specified position (optional operation). | |
void | clear() Removes all of the elements from this list (optional operation). | |
boolean | contains(Object o) Returns true if this list contains the specified element. | |
boolean | containsAll(Collection<?> c) Returns true if this list contains all of the elements of the specified collection. | |
boolean | equals(Object o) Compares the specified object with this list for equality. | |
E | get(int index) Returns the element at the specified position in this list. | |
int | hashCode() Returns the hash code value for this list. | |
int | indexOf(Object o) Returns the index in this list of the first occurrence of the specified element, or -1 if this list does not contain this element. | |
boolean | isEmpty() Returns true if this list contains no elements. | |
Iterator<E> | iterator() Returns an iterator over the elements in this list in proper sequence. | |
int | lastIndexOf(Object o) Returns the index in this list of the last occurrence of the specified element, or -1 if this list does not contain this element. | |
ListIterator<E> | listIterator() Returns a list iterator of the elements in this list (in proper sequence). | |
ListIterator<E> | listIterator(int index) Returns a list iterator of the elements in this list (in proper sequence), starting at the specified position in this list. | |
E | remove(int index) Removes the element at the specified position in this list (optional operation). | |
boolean | remove(Object o) Removes the first occurrence in this list of the specified element (optional operation). | |
boolean | removeAll(Collection<?> c) Removes from this list all the elements that are contained in the specified collection (optional operation). | |
boolean | retainAll(Collection<?> c) Retains only the elements in this list that are contained in the specified collection (optional operation). | |
E | set(int index, E element) Replaces the element at the specified position in this list with the specified element (optional operation). | |
int | size() Returns the number of elements in this list. | |
List<E> | subList(int fromIndex, int toIndex) Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. | |
Object[] | toArray() Returns an array containing all of the elements in this list in proper sequence. | |
| toArray(T[] a) Returns an array containing all of the elements in this list in proper sequence; the runtime type of the returned array is that of the specified array. |
Method Summary | |
---|---|
Comparator<? super E> | comparator() Returns the comparator associated with this sorted set, or null if it uses its elements' natural ordering. |
E | first() Returns the first (lowest) element currently in this sorted set. |
SortedSet<E> | headSet(E toElement) Returns a view of the portion of this sorted set whose elements are strictly less than toElement. |
E | last() Returns the last (highest) element currently in this sorted set. |
SortedSet<E> | subSet(E fromElement, E toElement) Returns a view of the portion of this sorted set whose elements range from fromElement, inclusive, to toElement, exclusive. |
SortedSet<E> | tailSet(E fromElement) Returns a view of the portion of this sorted set whose elements are greater than or equal to fromElement. |
实用工具包中的类