本文整理匯總了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]);
}