1 / 8

Array

Array . Must declare a variable to reference the array double [] mylist; // cannot double list[20]; Or double mylist[]; The declaration doesn’t allocate memory until you create an array by mylist = new double[200]; For( int I = 0; I < mylist.Length; i++) mylist[i] = 2*I;.

wilmet
Download Presentation

Array

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. Array • Must declare a variable to reference the array • double [] mylist; // cannot double list[20]; • Or double mylist[]; • The declaration doesn’t allocate memory until you create an array by • mylist = new double[200]; • For( int I = 0; I < mylist.Length; i++) • mylist[i] = 2*I;

  2. Array initializer • Combine declaring an array, creating an array, and initializing in one statement • double [] myList = {1.9, 2.9, 3.4, 3.5}; • When processing array elements, you will often use a for loop

  3. Passing array to a method • public static void doArray(int[] array) • { • for( int i = 0; i < array.Length; i++) • { • array[i] = array[i] +1; • } • } • Difference between passing the values of primitive data types and passing arrays • Primitive data type: passed by value • Array: passed by reference

  4. Searching array • Searching is the process of looking for a specific element in an array • Linear search approach compares the key element sequentially with each element in the array. • Continue to do so until the key matches an element in the array or the array is exhausted without a match being found

  5. Linear search method • Public class LinearSearch{ • public static int LinearSearch(int [] list, int key){ • for( int I = 0; I < list.length; i++) • { if ( key == list[i]) • return I; • } • return -1; • } • } • This method return the index where the key element found, and return -1 if not found

  6. Sorting array • Selection sort: suppose that you want to sort a list in ascending order. Selection sort finds the largest number in the list and place it last. It then finds the largest number remaining and places it next to the last, and so on until the list contains only a single number.

  7. Public class SelectionSort{ • public static void selectionSort( double[] list){ • for ( int I = list.legth-1; i>=1; i--){ • double currentMax = list[0]; • int currentMaxIndex = 0; • for(int j =1; j <=I;j++){ • if(currentMax <list[j]){ • currentMax = list[j]; • currentMaxIndex = j; • } • } • // swap • if ( currentMaxIndex != i){ • list[currentMaxIndex] = list[i]; • list[i] = currentMax; • } • } • } • }

  8. Assignment • Write a program that • Create an array of integer with size 2 • Pop input dialog twice to ask user to enter two numbers • Convert numbers to int and store in the array • Write a method to swap the array • Print out the new array ( terminal or messagedialog)

More Related