使用ArrayList類遇到的一些小問題

2021-09-24 05:37:16 字數 869 閱讀 2224

當每輸入乙個數字時,將他儲存在乙個陣列中,檢測有沒有輸入重複的,然後輸入了以下**

public

static

void

main

(string args)

}}}

然而結果就是無論我輸入乙個什麼樣的值他都會出現提示重複,like following

enter an integer: 4

enter an integer: 5

you've entered 5

enter an integer: 6

you've entered 6

enter an integer: 4

you've entered 4

enter an integer: 5

you've entered 5

然後我就不斷地測試,到底**除了問題,又是設斷點,又是加輸出,搞得一團糟就像下面一樣。

for

(int i =

0; i < list.

size()

; i++

)}

結果就是什麼也沒有發現。

我說我要冷靜,一定可以看出來的,然後突然靈光一現,太上老君終於明白了

問題就在for迴圈上

for 迴圈將所有arraylist中的元素全部遍歷了一遍,所以將剛剛加入的那個也遍歷了。將其取出就好了

哈哈哈哈哈哈哈,我真是個沙雕哈哈哈

public

static

void

main

(string args)

}}}

ArrayList類的用法

靈活的設定陣列的大小 int32 values int32 list.toarray typeof int32 返回arraylist包含的陣列 這是乙個簡單的例子,雖然沒有包含arraylist所有的方法,但是可以反映出arraylist最常用的用法 用指定的大小來初始化內部的陣列 如果使用arr...

C 中ArrayList類的使用方法

arraylist元素的新增 using system using system.collections.generic using system.linq using system.text using system.collections 在c 中使用arraylist必須引用collectio...

C 中ArrayList類的使用方法

arraylist元素的新增 using system using system.collections.generic using system.linq using system.text using system.collections 在c 中使用arraylist必須引用collectio...