Java函数库API
可以在Java API 在线文档网站上查询API文档,或者查阅参考书。最大的区别是参考书会告诉你方法需要什么参数,以及返回何种类型的数据,如果需要更多的相关细节,则需要查阅API文档。例如ArrayList,在参考书中你会知道它有一个indexOf()方法,indexOf()取用一个对象参数并返回int类型的索引值,而API文档会告诉你indexOf()在找不到相符对象的情况下会返回-1。
public int indexOf(Object o)
Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.
Specified by:
indexOf in interface List
Overrides:
indexOf in class AbstractList
Parameters:
o - element to search for
Returns:
the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element
ArrayList方法
ArrayList是java函数库数百个类中的一个类,可以把它们当作自己写的一样运用。
- add(Object elem) : 像list中加入对象参数
- remove(int index):在索引参数中移除对象
- remove(Object elem):移除该对象
- contains(Object elem):如果和对象参数匹配返回true
- isEmpty():如果list中没有元素返回true
- indexOf(Object elem):返回对象参数的索引或“-1”
- size():返回list中元素的一个数
- get(int index):返回当前索引参数的对象。
ArrayList操作举例
- 创建
ArrayList myList = new ArrayList(); - 加入元素
Egg s = new Egg();
myList.add(s); - 再继续加入元素
Egg b = new Egg();
myList.add(b); - 查询大小
int theSize = myList.size(); //返回2 - 查询指定元素
boolean isIn = myList.contains(s); //返回true - 查询特定元素的位置
int idx = myList.indexOf(b); //返回1 - 判断集合是否为空
boolean empty = myList.isEmpty(); //返回false - 删除元素
myList.remove(s); //myList被缩小了
ArrayList与一般数组的比较
ArrayList | 一般数组 |
---|---|
ArrayList myArrayList = new ArrayList (); | String[] myList = new String[2]; |
String a = new String(“ww”); | String a = new String(“ww”); |
myArrayList.add(a); | myList[0] = a; |
String b = new String(“hello”); | String b = new String(“hello”); |
myArrayList.add(b); | myList[1] = b; |
int theSize = myArrayList.size(); | int theSize = myList.length; |
Object o = myArrayList.get(1); | String o = myList[1]; |
myArrayList.remove(1); | myList[1] = null; |
boolean isIn = myArrayList.contains(b); | boolean isIn = false; for (String item : myList) { if (b.equals(item)) { isIn = true; break; } } |
- 一般数组在创建时就必须确定大小,但对于ArrayList来说,你只需要创建出此类型的对象就行。它不需要指定大小,因为它会在加入或删除元素时自动地调整大小。
new String[2] //指定大小
new ArrayList () //不需指定大小 - 存放对象给一般数组时必须指定位置
myList[1] = b; //指定索引值
myArrayList.add(b); //不需指定索引值 - 一般数组使用特殊的语法,但ArrayList是个普通对象,所以不会有特殊的语法。
myList[1] //[方括号]是只用在数组上的特殊语法 - 在java 5.0以上,ArrayList是参数化的 – 参数化类型
ArrayList // 是类型参数。这代表String的集合,就像ArrayList代表dogs的集合
超强布尔表达式
- 不等于 !=
- 非 !
- 短运算符: 与 && , 非 ||
以&&为例,如果java虚拟机发现左方的表达式为false,则它不需也不会去计算右方的算式才知道要返回false,||同理。 - 长运算符 : 按位与 & , 按位或 |
会强制java虚拟机一定要计算运算符两边的算式。
包
在java的API中,类是被包装在包中,java函数库中的梅格雷都属于某个包。ArrayList被放在java.util包中。你必须指明程序代码中所使用到的类的完整名称。
- import
放一个import述句在程序源文件的最前面:
import java.util.ArrayList;
public class MyClass {…}
不必import进String类或System类的原因是,java.lang是个预先被引用的包,java.lang是个经常会用到的基础包,所以不必指定名称,java.lang.String和java.lang.System是独一无二的class,java会知道要去哪里找。
- type
或者在程序代码中打出全名,不管在哪里,只要使用到,就打出全名。如果怕麻烦的话,就用import帮助你,import可以可以帮你省下每个类前面的包名称,程序不会因为import变大或变慢。- 声明的时候
java.util.ArrayList list = new java.util.ArrayList (); - 用在参数的时候
public void go (java.util.ArrayList list) {} - 作为返回类型的时候
public java.util.ArrayList foo() {}
- 声明的时候
包之所以很重要,有3个原因:
- 帮助组织项目或函数库相对于一大堆零散的类,以功能来组织会比较好。
- 包可以制造出命名空间,以便错开相同名称的类
- 包可以通过限制同一包之间的类才能相互存取以维护安全性