【发布时间】:2014-05-27 04:46:25
【问题描述】:
我刚开始学习 Java 中的 ArrayList 类。我有以下代码测试 ArrayList 类及其方法:
import java.util.ArrayList;
public class NewArrayList {
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<String> myList = new ArrayList<String>();
String s = new String();
myList.add(s);
String b = new String();
myList.add(b);
int theSize = myList.size();
System.out.println("ArrayList size: " + theSize);
boolean isTrue = myList.contains(s);
System.out.println(isTrue);
int whereIsIt = myList.indexOf(s);
System.out.println(whereIsIt);
int whereIsIt2 = myList.indexOf(b);
System.out.println(whereIsIt2);
}
}
indexOf 方法显示对象的索引。因此,由于我在 myList ArrayList 对象引用中添加了两个对象 s 和 b,因此它的索引内应该有 2 个对象。 whereIsit 和 whereIsit2 的输出都是 0。不应该是0 1吗??
【问题讨论】: