Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays and Strings A way to make oodles of variables, and a deeper look at classes.

Similar presentations


Presentation on theme: "Arrays and Strings A way to make oodles of variables, and a deeper look at classes."— Presentation transcript:

1 Arrays and Strings A way to make oodles of variables, and a deeper look at classes

2 Variables vs. arrays The variables we’ve looked at so far are all primitive types One variable holds one value An array holds several variables of the same type Its components are numbered, starting with 0 One array variable holds multiple values

3 Declaring and assigning arrays int[] fibonacci; fibonacci = new int[5]; fibonacci[0] = 1; fibonacci[1] = 1; fibonacci[2] = 2; fibonacci[3] = 3; fibonacci[4] = 5; We use brackets [] right after the variable type to indicate that we are declaring an array We use the word “new” to create new arrays We use index numbers within the brackets to refer to individual components of an array

4 Assigning values to arrays fibonacci[0] fibonacci[1] fibonacci[2] fibonacci[3] fibonacci[4] 1 1 2 3 5 [0] [1] [2] [3] [4]

5 Arrays and FOR loops It is often useful to use arrays and FOR loops together for assigning values to arrays and for outputting values of arrays int c; int[] naturals; naturals = int[5]; for ( c=0; c<5; c++) { naturals[c] = c+1; } for ( c=0; c<5; c++ ) { Std.out.println(naturals[c]); }

6 Use arrays! Write a program that asks the user for their five favorite numbers, and store those numbers in an array. Modify your program to ask the user for a number n, and then ask the user for their n favorite numbers, and store those numbers in an array.

7 Multi-dimensional arrays The arrays we have examined so far are only one-dimensional arrays You can create arrays in two, three, or more dimensions. Remember, the more dimensions your array is, the more memory they will require!

8 Declaring and assigning multi- dimensional arrays int[][] grid; grid = new int[2][3] We declare and assign multi-dimensional arrays the same way as one-dimensional arrays We use multiple sets of brackets to indicate the desired number of dimensions

9 Assigning values to multi- dimensional arrays [0][1] [1][1] [2][1] [0][0] [1][0] [2][0]

10 Arrays and FOR loops It is often useful to use nested FOR loops to assign values to multi- dimensional arrays int x,y; int[][] multtable; multtable = int[10][10]; for ( x=0; x<10; x++) { for ( y=0; y<10; y++ ) { multtable[x][y] = (x+1)*(y+1); }

11 What is a string? A string is any sequence of text, numbers, or text and numbers together A substring of a string is any sequence of text and/or numbers contained within the larger string

12 Strings in Java In Java, a string is an object variable We use a class built into the Java language called “String” We call the String class a standard class

13 Declaring and assigning strings int days; days = 31; String name; name = new String(“Matthew”); String automaton; automaton = new String(Std.in.readLine()); We use the word “new” and the constructor method of the String class to create new strings

14 Adding strings String firstName; String lastName; String fullName; firstName = new String(“William”); lastName = new String(“Gates”); fullName = new String(firstName + “ “ + lastName); We can “add” strings together using a plus sign

15 Outputting strings String firstName; String lastName; String fullName; firstName = new String(“William”); lastName = new String(“Gates”); fullName = new String(firstName + “ “ + lastName); Std.out.println(fullName); We can output strings using the Std.out.println command

16 What makes strings special? When we create a string, we are creating an instance of the standard class String Therefore, we use methods in the standard class to find out information about our string Think of the standard class String as a rubber stamper Each time we make a new string, it’s like making a stamp with all the properties of the original

17 Useful methods Method name Input type Output type Action s.length()noneintreturns the number of characters in s s.charAt(n)intcharreturns the character at position n in s s.substring(n)intStringreturns the substring from position n to the end of s s.substring(n,m)int, intStringreturns the substring from position n to position m-1

18 Using string methods import extra.*; public class NameReader { public static void main (String args[]) { String name; int x; Std.out.println(“What is your name?”); name = new String(Std.in.readLine); x = name.length(); Std.out.println(“Your name has “ + x + “ letters.”); }

19 Use some strings! Write a program that asks the user for his or her first name The program should store that name in a string and determine the first letter of the name and print that letter Modify your program to find the last letter of your user’s first name


Download ppt "Arrays and Strings A way to make oodles of variables, and a deeper look at classes."

Similar presentations


Ads by Google