- 언어 : C# (Windows forms)
- 환경 : 비주얼스튜디오
- 공부일자 : 2019-04-11
"ArrayList" 컬렉션 사용하기.
사용하기 위해선 using System.Collections; 선언.
ArrayList [ArrayList변수임시명] = new ArrayList();
객체 생성 후, Add(), Insert(), Remove(), RemoveAt() ... 등을 사용하면 된다.
using System;
using System.Windows.Forms;
using System.Collections;
//Collections라는 namespace안에 들어있다.
//ArrayList라는 컬렉션과같은 틀둘이들어있어 쓸 수 있음.
//이 안의 ArrayList는 Class.
namespace AnimalShelter
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ArrayList arrayList = new ArrayList();
//ArrayList의 컬렉션은 object의 변수를 저장.
arrayList.Add(0); //Add() 의 저장변수는 object.
//object변수는 모든 datatype과 class형태까지 들어가기 가능.
arrayList.Add(1);
arrayList.Add(2);
arrayList.Add(4);
/* arrayList[0] = 1;
* arrayList[1] = 2;
* arrayList[2] = 4;
* */
arrayList.Insert(2, 2);
//index 2의 자리에 2를 넣는다.
arrayList.Remove(1);
//()에 지울 값 자체를 넣는다.
arrayList.RemoveAt(2);
//()에 지울 index를 넣는다.
//arrayList[0];
int sum = 0;
for(int i=0; i<arrayList.Count; i++)
{
int num = (int)arrayList[i];
sum += num;
}
MessageBox.Show(sum.ToString());
}
}
}
'- C#' 카테고리의 다른 글
[DevExpress] GridControl 에서 RepositoryItem이 안보이는 현상 (1) | 2024.02.26 |
---|---|
[C#] Windows Form 폼 위에 다른 폼 띄우기. (Topmost, Owner, Show, ShowDialog, 모달, 모달리스) (0) | 2020.04.08 |
[C#] 메모장으로 저장, 저장한 메모장 파일 바로 열기. (0) | 2020.03.27 |
[C#] 배열 생성 / ArrayList와 List 컬렉션 사용해보기. (0) | 2019.04.15 |