Design and implement a class that uses an array to mimic the behavior of some of
ID: 3788648 • Letter: D
Question
Design and implement a class that uses an array to mimic the behavior of some of the methods in the ArrayList class. Include the following methods: Add(): Adds an element to the array. Clear(): Removes all elements from the array. Contains(): Determines if a specified item is in the array. Indexof(): Returns the index of the first occurrence of the specified item. Insert(): Insert an element into the array at a specified index. Remove(): Removes the first occurrence of the specified item. RemoveAt(): Removes an element at the specified index. Reverse(): Reverses the order of the elements in the array. Note, as you review the methods above, some may need arguments sent to them. For example Add() may need the object sent that is added to the data structure. Write a program to test your implementation. Maximum credit awarded for solutions that are readable, modularized, follow proper naming and spacing conventions. AFTER YOU COMPLETE YOUR WORK, close the solution, zip it and then upload the zipped solution to Blackboard under Course Documents > Exam l. Print your source code and a sample run. Recall you print a sample run by capturing the output screen using the Alt + PrtScn keys to place the active screen in the clipboard. Once it's in the clipboard, paste the clipboard's contents into a Word document using Ctrl + V. If there is still an issue with printing your source from within Visual Studio, you can also copy the program statements into the clipboard and paste that into Word.Explanation / Answer
first of all i am clear to you that arraylist is non-generic collection in c# it can store data of various type and grow with its size dynamically
for using arrayList functionality we must include system.collections namespace.
.net framework arraylist.add method add elements to the list and for accessing them we loop through it
.Net framework ArrayList.Clear() method allow us to remove all elements from the ArrayList.
dear i have problem with attaching document but i am sending code following please run it on visual studio it will run fine and have complete solution of your problem
class Program
{
static void Main(string[] args)
{
ArrayList arlist = new ArrayList();
arlist.Add("One");
arlist.Add("Two");
arlist.Add("Three");
// for print
Console.WriteLine("For Add Method ");
foreach (string s in arlist)
{
Console.WriteLine(s);
}
// for reverse : it reverse the object of list but before using it we sort the array list first by using sort method
Console.WriteLine(" for reverse mehtod");
arlist.Sort();
arlist.Reverse();
foreach (string s in arlist)
{
Console.WriteLine(s);
}
//for contains method which determines the purticular item are present in list or not it return boolean value
Console.WriteLine(" for contains mehtod");
bool result = arlist.Contains("Two");
Console.WriteLine(result);
// for indexof method provides the position of index that first occurance
Console.WriteLine(" for indexof mehtod");
int result1 = arlist.IndexOf("Two");
Console.WriteLine(result1);
// for insert : it insert an item on purticular index
Console.WriteLine(" for insert mehtod");
arlist.Insert(1, "Four");
foreach (string s in arlist)
{
Console.WriteLine(s);
}
// for remove : it remove the object
Console.WriteLine(" for remove mehtod");
arlist.Remove("One");
foreach (string s in arlist)
{
Console.WriteLine(s);
}
// for removeAt : it remove the object for purticular index
Console.WriteLine(" for removeAt mehtod");
arlist.RemoveAt(1);
foreach (string s in arlist)
{
Console.WriteLine(s);
}
//for clear: it clear all the element
Console.WriteLine(" For clear Method ");
arlist.Clear();
foreach(string s in arlist)
{
Console.WriteLine(s);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.