1 / 17

Arrays Lesson

Arrays Lesson. Ways to declare an array: i nt [] abc ; a bc = new int [500]; Or the combined way: Int [] abc = new int [500];. Arrays are objects and must be instantiated before being used. You can declare several arrays like this: int [] abc , xyz; abc = new int [500];

kenyon
Download Presentation

Arrays Lesson

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 Lesson

  2. Ways to declare an array: int[] abc; abc = new int[500]; Or the combined way: Int [] abc = new int[500]; Arrays are objects and must be instantiated before being used.

  3. You can declare several arrays like this: int[] abc, xyz; abc = new int[500]; xyz = new int [10]; or like this: int[] abc = new int[500], xyz = new int [10];

  4. Array variables are null before they are assigned array objects: int[] abc; Failure to assign an array object can result in a null pointer exception: int[] abc; abc[1]= 10; //run-time error; null pointer exception

  5. Because arrays are objects, two variables can refer to the same array: int[] abc, xyz; abc = new int[500] xyz = abc// xyz and abc now refer to the same array and thus have the same values If you want abc and xyz to refer to two separate arrays but contain the same values, what could you do?

  6. If we want abc and xyz to refer to separate arrays but have the same values, you could copy all of the elements from one array to the other: int[] abc, xyz; // declare two array variables inti; abc = new int[5] // Instantiate an array of size 5 for (i = 0; i < 5; i++) // Initialize the array abc[i] = i*i; // a[0]=0, a[1]=1, a[2]=4, etc. xyz = new int[5]; // Instantiate another array of size 5 for (i = 0; i < 5; i++) // initialize the second array xyz[i] = abc[i];

  7. Arrays can also be declared, instantiated and initialized in one step: int[] abc ={0, 1, 4, 9, 16} // Array size = 5 Other array formats: double[] ddd = new double[10]; char[] ccc = new char[10]; boolean[] bbb = new boolean[10]; String[] ggg = new String[10];

  8. One way to declare an array variable: int[] aaa; To declare more than one variable: int[] aaa, bbb, ccc; Another way to declare an array variable: intaaa[] intaaa[]; bbb; ccc; // Will not work! intaaa[]; bbb[]; ccc[]; // Will work

  9. String Review: public class StringReview{ public static void main(String[] args) { String str; String[] name = {"Bill", "Mary", "Jake", "Pete", "Susan"}; int[] laps = {1, 2, 3, 4, 5}; String[] phrase = new String[5]; phrase[3] = "The athlete ran x times around the track."; str = name[3] + phrase[3].substring(11, 16)+ laps[2] + phrase[3].substring(17); System.out.println(str); // what will the result be? } } Pete ran 3 times around the track.

  10. 5 Array abc above has a logical size of: 8 and a physical size (length) of: 3 how many cells contain garbage? To add elements to an array if empty cells are available: if (size < abc.length){ abc[size] = anInt; size++; }

  11. The Two-Dimensional Array int[][] column position row position How to assign values to a two-dimensional array using the Array Initializer: int[][] x = {{8,2,4}, {1,0,5}}; This is a two-row, three-column array named x

  12. How do you visualize this array? int[][] x = {{8,2,4}, {1,0,5}}; Column Indexes Row Indexes

  13. How do you access each of these elements? Column Indexes Array x Remember, state the row first, then column (Think: RC like in RC Cola ) Row Indexes

  14. import java.util.Scanner; public class TwoDimension { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int[][] x = new int[2][3]; for (int i=0; i<x.length; i++) { for (int j=0; j<x[0].length; j++) { System.out.print("Enter value for row " + i + ", col " + j + ": "); x[i][j] = scan.nextInt(); } // end for j }// end for i } } Declaring a 2-row by 3-column array Assigning a value to a specific element of array x: • Nested loops are common for two-dimensional arrays. • The outer for loop (variable i) iterates through each row. • The inner for loop (variable j), iterates through each column.

  15. Enhanced for Loop – • Relieves the programmer from having to initialize the first position, test the index for less than the last position of the array, and then increment the index. • Visits each array element from beginning to end. • On each pass through the loop, the current element is automatically assigned to a temporary variable (no loop required) for (<temporary variable declaration> : <array object>) <statement>

  16. Enhanced for Loop – for (<temporary variable declaration> : <array object>) <statement> int[] abc = {2, 3, 4}; int sum = 0; for (int element : abc) // element is a temporary variable sum += element; System.out.println (“First sum: “ + sum); On each pass through the loop, the integer at the current position in abc is automatically assigned to element. On each pass through the second element is automatically assigned to a temporary variable (no loop required)

  17. To add elements in a two-dimensional array using Enhanced for Loop – public class Enhanced { public static void main(String[] args) { int[][] table = {{2, 3, 4}, {2, 3, 4}, {2, 3, 4}}; int sum = 0; for (int[]row : table) // row is a temporary variable for (int element : row) // row is a temporary variable sum += element; System.out.println ("Second sum: " + sum); } } On each pass through the first loop, the two dimensional array’s current row is assigned to the temporary variable called “row”. The second loop reiterates though this array, assigning each integer at the current column integer to another temporary variable element. integer at the current position in abc is automatically assigned to element.

More Related