本文整理汇总了Java中java.util.AbstractCollection.toArray方法的典型用法代码示例。如果您正苦于以下问题:Java AbstractCollection.toArray方法的具体用法?Java AbstractCollection.toArray怎么用?Java AbstractCollection.toArray使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.AbstractCollection
的用法示例。
在下文中一共展示了AbstractCollection.toArray方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: test_toArray
import java.util.AbstractCollection; //导入方法依赖的package包/类
/**
* @tests java.util.AbstractCollection#toArray()
*/
public void test_toArray() {
AbstractCollection<String> ac = new AbstractCollection<String>() {
@Override
public Iterator<String> iterator() {
return new Iterator<String>() {
String[] values = new String[] {"0", "1", "2"};
int index;
public boolean hasNext() {
return index < values.length;
}
public String next() {
return values[index++];
}
public void remove() {
fail("remove should not get called");
}
};
}
@Override
public int size() {
return 3;
}
};
Object[] array = ac.toArray();
assertEquals(3, array.length);
for (Object o : array) {
if (!"0".equals(o) && !"1".equals(o) && !"2".equals(o)) {
fail("an unexpected element was removed");
}
}
}
示例2: test_toArray
import java.util.AbstractCollection; //导入方法依赖的package包/类
/**
* @tests java.util.AbstractCollection#toArray()
*/
public void test_toArray() {
AbstractCollection<String> ac = new AbstractCollection<String>() {
@Override
public Iterator<String> iterator() {
return new Iterator<String>() {
String[] values = new String[] {"0", "1", "2"};
int index;
public boolean hasNext() {
return index < values.length;
}
public String next() {
return values[index++];
}
public void remove() {
fail("remove should not get called");
}
};
}
@Override
public int size() {
return 3;
}
};
Object[] array = ac.toArray();
assertEquals(3, array.length);
for (Object o : array) {
if (!"0".equals(o) && !"1".equals(o) && !"2".equals(o)) {
fail("an unexpected element was removed");
}
}
}
示例3: iterator
import java.util.AbstractCollection; //导入方法依赖的package包/类
/**
* @tests java.util.AbstractCollection#toArray(java.lang.Object[])
*/
public void test_toArray$Ljava_lang_Object() {
AbstractCollection<String> ac = new AbstractCollection<String>() {
@Override
public Iterator<String> iterator() {
return new Iterator<String>() {
String[] values = new String[] {"0", "1", "2"};
int index;
public boolean hasNext() {
return index < values.length;
}
public String next() {
return values[index++];
}
public void remove() {
fail("remove should not get called");
}
};
}
@Override
public int size() {
return 3;
}
};
try {
ac.toArray(null);
fail("No expected NullPointerException");
} catch (NullPointerException e) {
// expected
}
String[] a = new String[3];
assertSame(a, ac.toArray(a));
a = new String[0];
assertNotSame(a, ac.toArray(a));
a = ac.toArray(a);
assertEquals(3, a.length);
CharSequence[] csa = new CharSequence[3];
ac.toArray(csa);
assertEquals(3, csa.length);
assertEquals("0", csa[0]);
assertEquals("1", csa[1]);
assertEquals("2", csa[2]);
}