Presentation is loading. Please wait.

Presentation is loading. Please wait.

Foundations of Programming: Arrays

Similar presentations


Presentation on theme: "Foundations of Programming: Arrays"— Presentation transcript:

1 Foundations of Programming: Arrays
September 28, 2016

2 Arrays: Arrays are a special type of variable that stores and manipulates large quantities of data. Arrays store data in variable that can hold more then one specific value. Arrays can store multiple values of the same type. Instead of creating 10 different variables to store the multiples of 10, I can instead create one int Array and store multiples of 10 in that one array.

3 Creation of Arrays: There are many ways to create an Array. Below I will show you one of the ways to create an array. int[] a = new int[10] ; // Here a created an array that can hold 10 unique values a[0] = 10; a[1] = 20; a[2] = 30; // And so on until the last unit of the array a[9] = 100; // The last index(9) you can use for an Array of size 10 is 9. Now you add brackets after the type (int, double, char, etc.). In this example I gave all of my indexes (0-9) in the array a value, but if you don’t do that those indexes will have ZERO values.

4 More Creation of Arrays in Java:

5 Creating an array with all predefined values:
Arrays can also be created with already defined values. If I wanted to store my entire name in a String array, I can do that as so: String[] myName = {“William”, “Jose”, “Gutierrez”}; The size of the array is 3, and the indexes are 0, 1, and 2. I can do this with all data types. If I wanted to create a double array that stores the first 5 prime numbers then I can do that as so: double[] primes = {1.0, 2.0, 3.0, 5.0, 7.0}; The size of the array is 5 and the indexes are 0, 1, 2, 3, and 4.

6 For Loops and Arrays: Arrays are used with for loops a lot because by using for loops I can display all the contents of an array fairly easy. You can use special parameter of arrays to get the length of a particular array. Knowing the length of an array, you can display all the contents of an array in a for loop The parameter is called .length If you have an array of size 4, then . length will return 4, but remember the indexes of that array are 0, 1, 2, and 3. If you try to access an index that is not located within an array, your program will crash because you are trying to access something that doesn’t exist. This is why we use .length

7 Do Now: Arrays and For Loops
Write the output of the program below:

8 Array Examples


Download ppt "Foundations of Programming: Arrays"

Similar presentations


Ads by Google