- If we create an array of object type , then it is possible to store items of different types.
- An Arraylist use its indexes to refer to a particular item stored in its collection.
- Declaring an ArrayList:-
- ArrayList objArrList=new ArrayList();
- Add Items in ArrayList collection in console application
whenever we will used
the ArrayList we need to
import one namespace
i.e.,”System.Collections”
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Text;
using
System.Collections;// import this namespace
namespace CAArrayList
{
class ClsArrayList
{
static
void Main(string[]
args)
{
ArrayList
objArrList = new ArrayList();
//Add
Items to the ArrayList collection
objArrList.Add("Sampath");
objArrList.Add("kumar");
objArrList.Add("vaddepally");
foreach
(Object objItem in
objArrList)
{
Console.WriteLine(objItem);
}
Console.Write("Enter a value to Add:");
object
objValue = Console.ReadLine();
objArrList.Add(objValue);
Console.WriteLine("---------After adding value---------\n Array list Elements Are");
foreach
(Object objItem in
objArrList)
{
Console.WriteLine(objItem);
}
Console.Read();
}
}
}
|
Output:-
Differences between Array & ArrayList
SNO
|
Array
|
Array List
|
1
|
int[
] A=new int[ ]{1,4,3};
|
ArrayList
objArrayList=new ArrayList();
|
2
|
Array
is in the System namespace
|
ArrayList
is in the System. Collections namespace
|
3
|
The
capacity of an Array is fixed
|
ArrayList
can increase and decrease size dynamically
|
4
|
Array
is a collection of similar items
|
An
Array is a collection of similar items
|
5
|
Array
can have multiple dimensions
|
ArrayList
always has exactly one dimension
|
No comments:
Post a Comment