190 likes | 202 Views
Learn about arrays, a way to store multiple variables of the same type, and explore strings, which allow you to manipulate text and numbers in Java.
E N D
Arrays and Strings A way to make oodles of variables, and a deeper look at classes
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 Variables vs. 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 Declaring and assigning arrays
Assigning values to arrays 1 1 2 3 5 [0] [1] [2] [3] [4] fibonacci[0] fibonacci[1] fibonacci[2] fibonacci[3] fibonacci[4]
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]); }
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.
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!
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 Declaring and assigning multi-dimensional arrays
Assigning values to multi-dimensional arrays [0][0] [1][0] [2][0] [0][1] [1][1] [2][1]
Arrays and FOR loops • It is often useful to use nested FOR loopsto 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); } }
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
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
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 Declaring and assigning 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 Adding 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 Outputting strings
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 What makes strings special?
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.”); } }
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 Use some strings!