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

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

Arrays.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Java Syntax Primitive data types Operators Control statements.
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
Arrays. A group of data with same type stored under one variable. It is assumed that elements in that group are ordered in series. In C# language arrays.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Variables, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 Wanda M. Kunkle.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Alice in Action with Java
Multi-Dimensional Arrays in Java "If debugging is the process of removing software bugs, then programming must be the process of putting them in." -- Edsger.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Program structure Four different storage-class specifications: –Automatic (auto): local to a function, normally declared variables are automatic. Does.
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Object Oriented Programming Lecture 5: Arrays and Strings Mustafa Emre İlal
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Java SE 8 for Programmers, Third Edition
Chapter 1 Object Orientation: Objects and Classes.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
Lecture 9 Using Objects. Remember: 3 Different Kinds of Classes 1.Application Class – what we've been doing – Has public static void main ( String []
Case study Students. Array of objects Arrays can hold objects (ref to objects!) Each cell in an array of objects is null by default Sample: from student.
DT249-Information Systems Research Practice Programming Revision Lecture 2 Lecturer: Patrick Browne.
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 1 Simple Python Programs Using Print, Variables, Input.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
VARIABLES Programmes work by manipulating data placed in memory. The data can be numbers, text, objects, pointers to other memory areas, and more besides.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
C Programming – Part 3 Arrays and Strings.  Collection of variables of the same type  Individual array elements are identified by an integer index 
Working With Objects Tonga Institute of Higher Education.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
Arrays An array is an indexed data structure which is used to store data elements of the same data type. An array is an indexed data structure which is.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
 An array stores multiple values in one single variable.  Example: Output: I like Honda Civic, BMW and Toyota.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
MULTI-DIMENSIONAL ARRAYS 1. Multi-dimensional Arrays The types of arrays discussed so far are all linear arrays. That is, they all dealt with a single.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
UFCFY5-30-1Multimedia Studio Coding for Interactive Media Fundamental Concepts.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Chapter 8 Arrays and the ArrayList Class Arrays of Objects.
Strings CSE 1310 – Introduction to Computers and Programming
Functions + Overloading + Scope
Strings CSE 1310 – Introduction to Computers and Programming
© 2016 Pearson Education, Ltd. All rights reserved.
Yanal Alahmad Java Workshop Yanal Alahmad
String Methods: length substring
EKT150 : Computer Programming
Strings A collection of characters taken as a set:
Class Examples.
Java SE 7 One and Multi Dimensional Arrays Module 6
Arrays Arrays A few types Structures of related data items
C++ Array 1.
Review for Midterm 3.
More on iterations using
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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