1 / 26

Arrays

Arrays. What is an array?. An array is a collection of variables all of the same type. The size of an array is set when it is instantiated (created). What is an array?. Arrays allow you to store several variables of the same type in one place. int[ ] ray = new int[9];

vala
Download Presentation

Arrays

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

  2. What is an array? • An array is a collection of variables all of the same type. • The size of an array is set • when it is instantiated (created).

  3. What is an array? Arrays allow you to store several variables of the same type in one place. int[ ] ray = new int[9]; ray stores 9 integer variables.

  4. Strings are arrays String s = "abcdefg"; //Strings are arrays 0 1 2 3 4 5 6 s The first index position in a String is 0. A String is a group of characters.

  5. Arrays int[] nums = new int[9]; //java int array 0 1 2 3 4 5 6 7 8 9 nums Arrays are filled with 0s when instantiated.

  6. Arrays int[] nums = new int[9]; //java int array 0 1 2 3 4 5 6 7 8 9 nums The first index position in a String is 0. The size must always be an int.

  7. Setting Array Spots 0 1 2 3 4 5 6 7 8 9 nums The index indicates which box/spot in the array is being manipulated. The plural of index is indicesorindexes. nums[0] = 9; The 0 spot is being set to 9.

  8. Index Java indexes must always be integers and the first index will always be 0. 0 1 2 3 4 5 6 7 8 9 nums

  9. //Array instantiation example import static java.lang.System.*; public class ArrayInit { public static void main(String args[]) { int[ ] zero = new int[10]; System.out.println(zero[1]); System.out..println("\n\n"); int[ ] nums = {1,2,3,4,5,6,7}; System.out.println(nums[0]); System.out.println(nums[1]); System.out.println(nums[2]); System.out.println(nums[5]); System.out.println("\n\n"); String[ ] words = new String[10]; words[0] = "abc"; words[4] = "def"; System.out.println(words[0]); System.out.println(words[4]); System.out.println(words[1]); int size=10; int[ ] ray2 = new int[size]; System.out.println(ray2[1]); System.out.println("\n\n"); int[ ] ray3 = null; /ray3 has no size System.out.println(ray3[1]); } } Cut and paste this codeinto Dr. Java. Save, compileand run the program. Make sure that you understandeach line of the program, and the output that is produced.

  10. Printing Arrays

  11. Printing array spots int[ ] nums = {1,2,3,4,5,6,7}; System.out.println(nums[0]); System.out.println(nums[1]); System.out.println(nums[2]); System.out.println(nums[5]); OUTPUT 1236

  12. Printing arrays int[ ] nums = {1,2,3,4,5,6,7}; for ( int i = 0; i < nums.length; i++) { System.out.println(nums[i]); } length returns the # of elements/items in the array!!! OUTPUT1234567

  13. Printing arrays int[] nums = {1,2,3,4,5,6,7}; for ( int i= 0; i <= 6; i++) { System.out.println(nums[i]); } OUTPUT1234567

  14. Array Output Example //Array output example public class ArrayOut1 { public static void main(String args[]) { int[] nums = {1,2,3,4,5,6,7}; System.out.println(nums[0]); System.out.println(nums[1]); System.out.println(nums[2]); System.out.println(nums[5]); } } Type the program above in Dr. Java and compile it and run it.Make sure you understand the output.

  15. Array Output Example #2 //Array output example public class ArrayOut2 { public static void main(String args[]) { int[] ray = {5,145,15,45,50,55,60,65,70}; for( int i=0; i<ray.length; i++) { System.out.println(ray[i]); } System.out.println("\n\n"); } } Cut and paste the program above in Dr. Java and compile it and run it.Make sure you understand the output.

  16. Setting Arrays

  17. Setting array spots int[] nums = new int[10]; nums[0] = 231; nums[4] = 756; nums[2] = 123; System.out.println(nums[0]); System.out.println(nums[1]); System.out.println(nums[4]); System.out.println(nums[4/2]); OUTPUT 2310756123

  18. Setting array spots double[] nums = new double[10]; nums[0] = 10.5; nums[3] = 98.6; nums[2] = 77.5; System.out.println(nums[0]); System.out.println(nums[3]); System.out.println(nums[7]); OUTPUT 10.598.60.0

  19. Setting array spots int[] nums = new int[4]; for ( int i = 0; i <nums.length; i++) { nums[i] = i*4; } 0 1 2 3 0 4 8 16 if size is 4 ray

  20. Instance Variables class Array { private int[] nums; //has the value null public Array(){ nums = new int[10]; //sizes the array } //other methods not shown }

  21. class Arrays import java.util.Arrays; Class Arrays has sorting and searching methods for arrays.

  22. sort int ray[] = {45,78,90,66,11}; Arrays.sort(ray); for(int i =0; i <= 4; i++) { System.out.println(num); } OUTPUT11 45 66 78 90

  23. search OUTPUT7 11 34 45 66 2 -2 int ray[] = {45,7,34,66,11}; Arrays.sort(ray); for(int i=0; i<ray.length; i++) System.out.println(ray[i]); System.out.println(Arrays.binarySearch(ray, 34)); System.out.println(Arrays.binarySearch(ray, 9));

  24. String arrays String[] words = new String[10]; words[0] = "abc"; words[4] = "def"; System.out.println(words[0]); System.out.println(words[4]); System.out.println(words[1]); OUTPUTabcdefnull

  25. split String s = "one two four five"; String[] words = s.split(" "); System.out.println(words[0]); System.out.println(words[1]); System.out.println(words[3]); OUTPUTonetwofive

More Related