Collection

1.What is an Array ? What are different ways of writing an Array ?
Array is a datastructure that stores a collection of value that are of same data type.
diff ways of writing an array are-
a)specify the data length in declaration.
int [] count= new int[5];

b) int[] count = new int[] { 1, 2, 3 };
skip the length but have to specify the value.

c)int[] count = new int[2] { 1, 2, 3 };
both a and b. one thing should be taken care here the length of int array should be equal
to number of values specified.


2.Array is reference type or value type ?
Reference Type 

3.How do you search an item in an Array ?
The BinarySearch static method of Array class can be used to search for an item in a array. . The method takes at least two parameters - an array and an object (the item you are looking for). If an item found in an array, the method returns the index of the item (based on first item as 0th item),  else method returns a negative value.
eg- 
 string[] name = new string[] { "chai", "alex", "dave" };         
int i=Array.BinarySearch(name, "dfsdf");  

i returns a negative value.
 
4.How to achieve Deep copy and Shallow copy using Array ?
Deep Copy-- By using Array.CopyTo
Shallow Copy---Array.Clone()


5.What are different types of an Array ?
a) Single dimensional Array
b) Multidimensional Array
c) Jaggd Array


6.Can diff data types be stored in a single Array?
No
7.Where does array index starts ?
Array index starts at Zero.
Suppose you have int [] count= {1,2,3} then
int[0] will give you "1".

8.Give an example of accessing Array using foreach loop.
 string[] name = new string[] { "chai", "alex", "dave" };           
 foreach (string a in name)

{
    
  Console.WriteLine(a.ToString());               
}

9. How do you sort an item in Array in reverse order ?
Use Count and Reverse property of an array.
eg-
int[] count = new int[] { 1, 2, 3,6,8,4 };
 Array.Sort(count);
 Array.Reverse(count);


10.Find the maximum no in an array.
you can use the property of array called Max or use the below algo-


11.Convert arraylist to string array 
Suppose you have a arraylist say with 3 string values- a[0]-name,a[1]-age,a[2]-gender.After converting using below code it will look like

string.Join(",",(string[])a.ToArray(typeof(string)))

string a=name,age,gender

 




2 comments:

  1. 6.Can diff data types be stored in a single Array?

    Its yes, it can be stored

    ReplyDelete