1 / 10

Arrays and Strings

Arrays and Strings. CSCI293 - C# September 26, 2005. Refresher : declaring arrays. int[] myarray = new int [10]; Step One - declare myarray to be a variable holding a list of integers Step Two - allocate space for myarray. The foreach Statement. float[] bob = {1.1f, 2.2f, 3.3f, 4.4f,

kagami
Download Presentation

Arrays and Strings

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Arrays and Strings CSCI293 - C# September 26, 2005

  2. Refresher: declaring arrays int[] myarray = new int [10]; Step One - declare myarray to be a variable holding a list of integers Step Two - allocate space for myarray

  3. The foreach Statement float[] bob = {1.1f, 2.2f, 3.3f, 4.4f, for (int i=0; i < bob.Length; i++) Console.WriteLine ("{0}", bob[i]); foreach (float x in bob) Console.WriteLine ("{0}", x);

  4. Multi-Dimensional Arrays • Each dimension of a multi-dimension array can be the same (like in C++) • for example, 5 rows each of length 10 int[][] myArray = new int[5][10]; • Each dimension can be a different size • first row's length is 10, but second row's length is 15 int[][] myWeirdArray = new int[2][]; myWeirdArray[0] = new int[10]; myWeirdArray[1] = new int[15];

  5. Passing Arrays as Parameters public int largest (params int[] thelist) { int max = thelist[0]; foreach (int value in thelist) if (value > max) max = value; return max; } Then inside main(): Class1 tempvar = new Class1(); int largest; int[] myarray = {8,2,3,9,4}; largest = tempvar.largest (myarray); Console.WriteLine ("largest = {0}", largest); largest = tempvar.largest (10, 40, 20, 30); Console.WriteLine ("largest = {0}", largest);

  6. Sorting and Searching Array.Sort (myarray); Console.Write ("sorted array = "); foreach (float value in myarray) Console.Write("{0} ",value); Console.WriteLine(); int index = Array.BinarySearch(myarray,4); Console.WriteLine("4 is at array index {0}", index);

  7. Other Array Functions // you don't need a for loop to copy an array Array.Copy(oldarray,0,newarray,0,oldarray.Length); // reverse an array Array.Reverse (myarray); //how long is the array? for (int i=0; i<myarray.Length; i++) // where is the value 99 in my list? location = Array.IndexOf (myarray, 99);

  8. Creating Strings string name1 = "Steve Dannelly"; string name2 = "BOB";

  9. Comparing Strings Unlike C++, where you must use strcmp to compare the contents of two strings, in C# you can use == if (name2 == name2.ToUpper()) Console.WriteLine("all uppercase"); else Console.WriteLine("not uppercase"); But, you still can not use < or > if (string.Compare(name1,name2) < 0) Console.Write("name1 < name2");

  10. Other String Functions • Length • Copy • PadLeft • PadRight • + • Note that "string" ≠ "char[]". So the list of string methods is different from the list of array methods • for example, there is no string.reverse • but you can do this: mystring[0] = 'A';

More Related