1 / 19

Arrays and Strings

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.

wmorales
Télécharger la présentation

Arrays and Strings

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 and Strings A way to make oodles of variables, and a deeper look at classes

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

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

  4. Assigning values to arrays 1 1 2 3 5 [0] [1] [2] [3] [4] fibonacci[0] fibonacci[1] fibonacci[2] fibonacci[3] fibonacci[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. 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

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

  10. 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); } }

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

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

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

  16. 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?

  17. Useful methods

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

More Related