當前位置: 首頁>>代碼示例>>Java>>正文


Java AbstractCollection.toArray方法代碼示例

本文整理匯總了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");
        }
    }
}
 
開發者ID:Sellegit,項目名稱:j2objc,代碼行數:40,代碼來源:AbstractCollectionTest.java

示例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");
        }
    }
}
 
開發者ID:keplersj,項目名稱:In-the-Box-Fork,代碼行數:40,代碼來源:AbstractCollectionTest.java

示例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]);
}
 
開發者ID:Sellegit,項目名稱:j2objc,代碼行數:52,代碼來源:AbstractCollectionTest.java


注:本文中的java.util.AbstractCollection.toArray方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。