260 likes | 348 Views
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];
E N D
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]; ray stores 9 integer variables.
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.
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.
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.
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.
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
//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.
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
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
Printing arrays int[] nums = {1,2,3,4,5,6,7}; for ( int i= 0; i <= 6; i++) { System.out.println(nums[i]); } OUTPUT1234567
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.
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.
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
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
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
Instance Variables class Array { private int[] nums; //has the value null public Array(){ nums = new int[10]; //sizes the array } //other methods not shown }
class Arrays import java.util.Arrays; Class Arrays has sorting and searching methods for arrays.
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
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));
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
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