How to use array list (ArrayList) in C#

7 Answers

0 votes
using System;
using System.Collections;
 
class Program
{
    static void Main() {
        ArrayList al = new ArrayList();
        
        al.Add("c#");
        al.Add("c");
        al.Add("java");
        al.Add("php");
        al.Add("python");

        foreach (object item in al) {
           Console.WriteLine(item);
        }
    }
}
 
 
/*
run:
 
c#
c
java
php
python
 
*/

 



answered Sep 11, 2019 by avibootz
edited Sep 5, 2023 by avibootz
0 votes
using System;
using System.Collections;
 
class Program
{
    static void Main() {
        ArrayList al = new ArrayList();
        
        al.Add("c#");
        al.Add("c");
        al.Add("java");
        al.Add("php");
        al.Add("python");

        foreach (object item in al) {
           Console.WriteLine(item);
        }
        
        Console.WriteLine("\n");
        
        al.Insert(0, "c++");
        al.Insert(3, "vb.net");
        
        foreach (object item in al) {
           Console.WriteLine(item);
        }
    }
}

 
 
/*
run:
 
c#
c
java
php
python


c++
c#
c
vb.net
java
php
python
 
*/

 



answered Sep 11, 2019 by avibootz
edited Sep 5, 2023 by avibootz
0 votes
using System;
using System.Collections;
 
class Program
{
    static void Main() {
        ArrayList al = new ArrayList();
        
        al.Add("c#");
        al.Add("c");
        al.Add("java");
        al.Add("php");
        al.Add("python");

        foreach (object item in al) {
          Console.WriteLine(item);
        }
        
        Console.WriteLine("\n");
        
        al.Remove("java");
        
        foreach (object item in al) {
          Console.WriteLine(item);
        }
    }
}

 
 
/*
run:
 
c#
c
java
php
python


c#
c
php
python
 
*/

 



answered Sep 11, 2019 by avibootz
0 votes
using System;
using System.Collections;
 
class Program
{
    static void Main() {
        ArrayList al = new ArrayList();
        
        al.Add("c#");
        al.Add("c");
        al.Add("java");
        al.Add("php");
        al.Add("python");

        foreach (object item in al) {
          Console.WriteLine(item);
        }
        
        Console.WriteLine("\n");
        
        al.RemoveAt(2);
        
        foreach (object item in al) {
          Console.WriteLine(item);
        }
    }
}

 
 
/*
run:
 
c#
c
java
php
python


c#
c
php
python
 
*/

 



answered Sep 11, 2019 by avibootz
0 votes
using System;
using System.Collections;
 
class Program
{
    static void Main() {
        ArrayList al = new ArrayList();
        
        al.Add("c#");
        al.Add("c");
        al.Add("java");
        al.Add("php");
        al.Add("python");

        foreach (object item in al) {
          Console.WriteLine(item);
        }
        
        Console.WriteLine("\n");
        
        al.RemoveRange(1, 3);
        
        foreach (object item in al) {
          Console.WriteLine(item);
        }
    }
}

 
 
/*
run:
 
c#
c
java
php
python


c#
python
 
*/

 



answered Sep 11, 2019 by avibootz
0 votes
using System;
using System.Collections;
 
class Program
{
    static void Main() {
        ArrayList al = new ArrayList();
        
        al.Add("c#");
        al.Add("c");
        al.Add("php");
        al.Add("java");
        al.Add("python");

        foreach (object item in al) {
          Console.WriteLine(item);
        }
        
        Console.WriteLine("\n");
        
        al.Sort();
        
        foreach (object item in al) {
          Console.WriteLine(item);
        }
    }
}

 
 
/*
run:
 
c#
c
php
java
python


c
c#
java
php
python
 
*/

 



answered Sep 11, 2019 by avibootz
0 votes
using System;
using System.Collections;
  
class Program
{
    static void Main() {
        ArrayList al =  new ArrayList{"c#", "c", "php", "java", "python", "vb.net"};

        Console.WriteLine(al.Count);
    }
}
 
  
  
/*
run:
  
6
  
*/

 



answered Sep 11, 2019 by avibootz
edited Dec 22, 2021 by avibootz

Related questions

2 answers 275 views
275 views asked Feb 9, 2017 by avibootz
1 answer 219 views
1 answer 136 views
1 answer 180 views
180 views asked Sep 10, 2020 by avibootz
1 answer 191 views
1 answer 127 views
127 views asked Apr 20, 2020 by avibootz
1 answer 194 views
194 views asked Apr 30, 2017 by avibootz
...