1 / 29

Arrays

Arrays. What is an array?. An array is a group of items all of the same type which are accessed through a single identifier. int[] nums = new int[ 10 ];. 0 1 2 3 4 5 6 7 8 9. nums. Array References. int[] nums;. nums. null. null. nothing.

lorne
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 group of items all of the same type which are accessed through a single identifier. int[] nums = new int[10]; 0 1 2 3 4 5 6 7 8 9 nums

  3. Array References int[] nums; nums null null nothing nums is a reference to an integer array.

  4. Array Instantiation new int[3]; 0x213 arrays are Objects.

  5. Arrays int[] nums = new int[3]; nums 0x213 0x213 nums is a reference to an integer array.

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

  7. Arrays int[] nums = new int[10]; //Java int array 0 1 2 3 4 5 6 7 8 9 nums Arrays are filled with 0 values when instantiated. The exact value of each spot in the array depends on the specified type for the array.

  8. Arrays int[] nums = new int[10]; //Java int array 0 1 2 3 4 5 6 7 8 9 nums The size must always be an int.

  9. Arrays int[] nums = {2,7,8,234,745,1245}; 0 1 2 3 4 5 nums An array can be initialized with values.

  10. Indexes 0 1 2 3 4 5 6 7 8 9 nums The [spot/index] indicates which value in the array is being manipulated. nums[0] = 9; The 0 spot is being set to 9.

  11. Indexes 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

  12. Array Constant Length • Unlike String who uses a method to return the length of the string… String name = “Furman”; int len = name.length(); //len is 6 • Arrays use a length constant. The length of an array can not change once it is created. int[] nums = { 1, 2, 3}; int numsLen = nums.length; // len = 3 • Notice that the method length() is followed by parenthesis, while the constant length is not!!

  13. Printing Arrays

  14. Printing arrays 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]); OUTPUT1236 0 1 2 3 4 5 6 nums

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

  16. Printing arrays int[] nums = {1,2,3,4,5,6,7}; for(int item : nums) { System.out.println(item); } OUTPUT1234567 0 1 2 3 4 5 6 nums

  17. //array output example 1 import static java.lang.System.*; public class ArrayOutOne { 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]); } }

  18. //array output example 2 import static java.lang.System.*; public class ArrayOutTwo { public static void main(String args[]) { int[] nums = {1,2,3,4,5,6,7}; //add in a for loop to print out nums System.out.println("\n\n"); //another bug to fix - what is the problem? int[] intList = null; //add in a for loop print out intList } }

  19. Setting Array Spots

  20. 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

  21. 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

  22. Setting array spots int[] nums = new int[6]; for(int spot=0; spot<nums.length; spot++) { nums[spot] = spot*4; } 0 1 2 3 4 5 nums

  23. //array set example 1 import static java.lang.System.*; public class ArraySetOne { public static void main(String[] args) { int[] nums = new int[10]; //all spots set to zero to start 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]); } }

  24. //array set example 2 import static java.lang.System.*; public class ArraySetTwo { public static void main(String[] args) { int[] nums = new int[6]; for(int spot=0; spot<nums.length; spot++) { nums[spot] = spot*4; } for(int spot=0; spot<nums.length; spot++) { System.out.println(nums[spot]); } } }

  25. Arrays as Instance Variables

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

  27. toString() public class Array { //instance vars and other methods not shown public String toString() { String output= ""; for(int spot=0; spot<nums.length; spot++) { output=output+nums[spot]+" "; } return output; } }

  28. toString() public class Array { //instance vars and other methods not shown public String toString() { String output= ""; for( int val : nums ) { output = output + nums + " "; } return output; } }

  29. InstanceVarsTwo String list = "7 6 3 4 9 1 3 5"; int[] nums = new int[8]; Scanner chopper = new Scanner(list); int spot=0; while(chopper.hasNextInt()) { nums[spot++]=chopper.nextInt();}

More Related