Arrays.

Slides:



Advertisements
Similar presentations
Chapter 8: Arrays.
Advertisements

Computer Programming w/ Eng. Applications
An Array A sequence of elements of a particular type Each element in the array has an index which gives its position in the sequence An array is declared.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Topic 9 – Introduction To Arrays. CISC105 – Topic 9 Introduction to Data Structures Thus far, we have seen “simple” data types. These refers to a single.
Arrays Liang, Chpt 5. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
 Pearson Education, Inc. All rights reserved Arrays.
CS0007: Introduction to Computer Programming Introduction to Arrays.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
COM S 207 While-Loop Statement Instructor: Ying Cai Department of Computer Science Iowa State University
CS100Lecture 101 Announcements Assignment P2 is due on Thursday Assignment P3 is handed out today Prelim on Monday the 19th. Coming soooooooooon.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
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.
Introduction to Collections Arrays. Collections Collections allow us to treat a group of values as one collective entity. The array is a collection of.
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.
Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing.
Operator precedence.  Evaluate a + b * c –multiplication first? a + (b * c) –addition first? ( a + b) * c  Java solves this problem by assigning priorities.
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
Special Methods in Java. Mathematical methods The Math library is extensive, has many methods that you can call to aid you in your programming. Math.pow(double.
Arrays. What is an array? An array is a collection of data types. For example, what if I wanted to 10 different integers? int num1; int num2; int num3;
Introduction to programming in java Lecture 21 Arrays – Part 1.
Loops Review. How to generate random numbers Math.random() will return a random decimal value in the form of a double. double num1 = Math.random(); num1.
Chapter 3 Using Variables, Constants, Formatting Mrs. UlshaferSept
CSC 211 Java I for loops and arrays.
Arrays.
Some Assignments  Write a program which prints the following information about at least 5 persons: NAME MAIL-ID EMPLOYEE-CODE PHONE Eg. Umesh
4. Java language basics: Function
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
FOP: JavaScript Arrays(Lists)
Two Dimensional Array Mr. Jacobs.
Introduction to Python Data Types and Variables
5. Function (2) and Exercises
Review of C… The basics of C scanf/printf if/elseif statements
ICS103 Programming in C Lecture 3: Introduction to C (2)
Arrays & Functions Lesson xx
Manipulating Pictures, Arrays, and Loops part 2
Arrays An Array is an ordered collection of variables
Chapter 6 Arrays Solution Opening Problem
More Loops.
More Loops.
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Unit-1 Introduction to Java
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
int [] scores = new int [10];
Defining methods and more arrays
For Loops.
Coding Concepts (Data- Types)
CS2011 Introduction to Programming I Arrays (I)
Announcements Lab 7 due Wednesday Assignment 4 due Friday.
Arrays ICS2O.
Lecture 13: Two-Dimensional Arrays
int [] scores = new int [10];
Lecture 10: Arrays AP Computer Science Principles
Arrays and Array Lists CS 21a.
Announcements Lab 6 was due today Lab 7 assigned this Friday
Building Java Programs
Dr. Sampath Jayarathna Cal Poly Pomona
Suggested self-checks: Section 7.11 #1-11
Data Structures & Algorithms
Building Java Programs
Agenda Packages and classes Types and identifiers Operators
Variables in C Topics Naming Variables Declaring Variables
Dry Run Fix it Write a program
First Semester Review.
Presentation transcript:

Arrays

What is an array? An array is a collection of data types. For example, what if I wanted to 10 different integers? int num1; int num2; int num3; int num4; int num5; int num6; int num7; int num8; int num9; int num10;

What is an array? int num1; int num2; int num3; int num4; int num5; int num6; int num7; int num8; int num9; int num10; This is very inefficient to write. What if we needed 100 elements? What about 1000?

What is an array? An array allows us to declare what type of data we want and how many of them we want. So if we need 10 integers, this is how we want to declare an array: int[ ] array1 = new int[10];

What is an array? What type of data name of the array variable int[ ] array1 = new int[10]; reserved word new size of the array

Decomposition of an Array int[ ] array1 = new int[10]; The size of an array must be declared before execution. This is not legal: int[ ] array1 = new int [x] if x is undefined.

Decomposition of an Array You can also assign an array in this manner. int[ ] array1 = {1, 5, 0, 6, 9, 1, 5, 3}; The size of the array above is 8 because it has 8 elements.

Accessing an Array Suppose I made the following declaration: int[ ] array1 = new int[10]; The size of the array above is 10. However, when I access these fields, the starting position is actually 0, meaning the final position is 9.

Accessing an Array int[ ] array1 = {1, 5, 0, 6, 9, 1, 5, 3}; // size is 8 position 0 position 4 position 7 What is position 1? What is position 6?

Accessing an Array int[ ] array1 = new int[10]; array1[0] = 56; array1[1] = 80; array1[2] = 74; array1[3] = 96; array1[4] = 71; array1[5] = 15;

Accessing an Array int[ ] array1 = new int[10]; // suppose all of these have values System.out.println(array[0]); int num1 = array1[7]; array1[8] = array1[4];

Assiging Arrays int[ ] array1 = new int[10]; So we have 10 elements. Let’s say we wanted to assign the value 100 to all elements. Are we going to write 10 of these statements: array1[0] = 100; array1[1] = 100; array1[2] = 100; array1[3] = 100; array1[4] = 100; // keeps going

Assiging Arrays int[ ] array1 = new int[10]; Enter the for loop for(int k = 0; k < 10; k++) array1[k] = 100; For loops are best at assigning values to arrays.

Assiging Arrays int[ ] array1 = new int[10]; However, you must be careful not to access elements that does not exist. for(int k = 0; k <= 10; k++) array1[k] = 100; Does array1[10] exist?

Assiging Arrays int[ ] array1 = new int[10]; What happens when you try to access elements that does not exist? array1[50]? Java will throw an error at you letting you know that the index of your array is out of bounds.

Assiging Arrays int[ ] array1 = new int[10]; Assigning: for(int k = 0; k < 10; k++) array1[k] = 100; Printing: System.out.println(array1[k] + “ “);

Array methods int[ ] array1 = new int[10]; array1.length will return the size of the array as an integer. Assigning: for(int k = 0; k < array1.length; k++) array1[k] = 100; Printing: System.out.println(array1[k] + “ “);

Review double[ ] name = new double[50]; String[ ] name2 = new String[40]; int[ ] name3 = new int[100]; for(int k = 0; k < name3.length; k++) name3[k] = 500; System.out.print(name3[k] + “ “);

Exercise 1 Declare an integer array of size 20. Use a for loop to initialize all values to 100. Use another for loop to print out the values.

Exercise 2 Declare a double array of size 10. Assign random values between 0-100 to each of these elements. Print out the values of these arrays.

Exercise 3 Declare a double array of size 20. Assign random values between 0-100 to each of these elements. Then go through the array and display its square root. Display the values of your array.

Review: int[ ] array1 = new int[10]; array[20] = 50; for(int k = 0; k < 10; k++) array1[k] = Math.random()*100; System.out.println(array1[k+1] + “ “);

What is the output? int[ ] array1 = new int[10]; for(int k = 0; k < array1.length; k++) array1[k] = k; System.out.print(array1[k] + “ “); Output: 0 1 2 3 4 5 6 7 8 9

What is the output? int[ ] array1 = new int[10]; for(int k = 0; k < array1.length; k++) array1[k] = array[k+1]; System.out.print(array[k] + “ “); Output: Error, array index out of bounds

What is the output? int[ ] array1 = new int[10]; for(int k = 5; k < array1.length; k++) array1[k] = k; for(int k = 0; k < array1.length; k++) System.out.print(array[k] + “ “); Output: 0 0 0 0 0 5 6 7 8 9

What is the output? int[ ] array1 = new int[10]; for(int k = 1; k < array1.length; k++) array1[k-1] = k*k; for(int k = 0; k < array1.length; k++) System.out.print(k + “ “); Output: 1 4 9 16 25 36 49 64 81 0

Exercise 4 Declare a double array of size 10. Assign random values between 0-100 to each of these elements. Then go through the array and determine which numbers are odd. Add +1 to every odd number and flip it to even. Display the values of your array.

Exercise 5 Declare an array of Strings. Let’s say a size of 5 should be fine. Get input for all those strings. Ask them to input only lower case letters. Once done, change the string to all upper case and display them. Example dog DOG cat CAT mouse MOUSE You will need the string method toUpperCase(). This method returns the string value attached to it and turns everything uppercase.

Exercise 6 Declare a double array of size 20. Assign values between 0-400 to each of them. Values should increase by 20 every array. So position 0 should be 0 and position 19 should be 380. Now display the array in reverse order. So start displaying from 380 to 0.

Exercise 7 Generate a 10 digit number. Separate each digit and store them in an array of 10 size long. Now, display the number in reverse order which should display the number in reverse.

Exercise 8 Shift Arrays exercise. Take an array of random integers between 0-10 and display this array. Now take this array and shift them by 5 cells to the left each. Note the following example. Array 1: 5 9 8 7 4 2 3 5 6 1 Shift: 2 3 5 6 1 5 9 8 7 4 Display your shifted array.