Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Chapter 8: Arrays.
Arrays. What is an array An array is used to store a collection of data It is a collection of variables of the same type.
8-May-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
 Demonstrate use of a “for loop” in the design and development of a Java program to calculate the total of a one-dimensional array of 6 integers.  Design.
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.
Additional control structures. The if-else statement The if-else statement chooses which of two statements to execute The if-else statement has the form:
Arrays Horstmann, Chapter 8. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
16-Jun-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 22 / 2008 Instructor: Michael Eckmann.
Loops – While, Do, For Repetition Statements Introduction to Arrays
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
CS 106 Introduction to Computer Science I 02 / 20 / 2008 Instructor: Michael Eckmann.
Lecture 5 Arrays A way to organize data © MIT AITI 2003.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
CS 106 Introduction to Computer Science I 02 / 19 / 2007 Instructor: Michael Eckmann.
The switch Statement, DecimalFormat, and Introduction to Looping
CS0007: Introduction to Computer Programming Introduction to Arrays.
1 BUILDING JAVA PROGRAMS CHAPTER 3 THE SCANNER CLASS AND USER INPUT.
Week 2 - Friday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Java Unit 9: Arrays Declaring and Processing Arrays.
Week 10 - Monday.  What did we talk about last time?  Method overloading  Lab 9.
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.
Hello AP Computer Science!. What are some of the things that you have used computers for?
Lecture 12 Instructor: Craig Duckett ARRAYS. Announcements Assignment 3 Assignment 3 Revision Assignment 4 (and Final Exam) GRADED! RETURNED! Woot! NEXT.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
Pemrograman Dasar Arrays PTIIK - UB. Arrays  An array is a container object that holds a fixed number of values of a single type.  The length of an.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
1 BUILDING JAVA PROGRAMS CHAPTER 7.1 ARRAY BASICS.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
CS107 References and Arrays By Chris Pable Spring 2009.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
VARIABLES Programmes work by manipulating data placed in memory. The data can be numbers, text, objects, pointers to other memory areas, and more besides.
1-Dec-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Working With Objects Tonga Institute of Higher Education.
Hello Computer Science!. Below is an example of a Hello World program in JAVA. While it is only three lines of code, there are many things that are happening.
CSE 1201 Object Oriented Programming ArrayList 1.
CS 31 Discussion, Week 5 Faisal Alquaddoomi, Office Hours: BH 2432, MW 4:30-6:30pm, F 12:00-1:00pm (today)
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
BIT115: Introduction to Programming
Introducing Arrays. Too Many Variables?  Remember, a variable is a data structure that can hold a single value at any given time.  What if I want to.
Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java.
int [] scores = new int [10];
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
FOR LOOPS "LOOP FOR A SET NUMBER OF TIMES.". FOR ( START_VALUE; END_VALUE; INCREMENT_NUMBER ) { //YOUR_CODE_HERE } So after the word "for" (in lowercase)
Introduction to Arrays. Learning Objectives By the end of this lecture, you should be able to: – Understand what an array is – Know how to create an array.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Lecture 11 Instructor: Craig Duckett Instance Variables.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
BIT 115: Introduction To Programming
The switch Statement, and Introduction to Looping
Java Language Basics.
Data Structures – 1D Lists
BIT115: Introduction to Programming
Arrays in Java.
Additional control structures
Week 7 - Monday CS 121.
Presentation transcript:

Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.

Arrays are like a spreadsheet To set up an array of number like that in the image above, you have to tell Java what kind of data is going in to your array (integers, strings, boolean values, etc). You then need to say how many positions the array has. You set them up like this: int[ ] aryNums; The only difference between setting up a normal integer variable and an array is a pair of square brackets after the data type. The square brackets are enough to tell Java that you want to set up an array. The name of the array above is aryNums.

But this just tells Java that you want to set up an integer array. It doesn't say how many positions the array should hold. To do that, you have to set up a new array object: aryNums = new int[6]; You start with your array name, followed by the equals sign. After the equals sign, you need the Java keyword new, and then your data type again. After the data type come a pair of square brackets. In between the square brackets you need the size of the array. The size is how many positions the array should hold. If you prefer, you can put all that on one line: int[ ] aryNums = new int[6];

So we are telling Java to set up an array with 6 positions in it. After this line is executed, Java will assign default values for the array. Because we've set up an integer array, the default values for all 6 positions will be zero ( 0 ).

To assign values to the various positions in an array, you do it in the normal way: aryNums[0] = 10; Here, a value of 10 is being assigned to position 0 in the array called aryNums. Again, the square brackets are used to refer to each position. If you want to assign a value of 14 to array position 1, the code would be this: aryNums[1] = 14; And to assign a value of 36 to array position 2, it's this: aryNums[2] = 36; Don't forget, because arrays start at 0, the third position in an array has the index number 2.

If you know what values are going to be in the array, you can set them up like this instead: int[ ] aryNums = { 1, 2, 3, 4 }; This method of setting up an array uses curly brackets after the equals sign. In between the curly brackets, you type out the values that the array will hold. The first value will then be position 0, the second value position 1, and so on. Note that you still need the square brackets after int, but not the new keyword, or the repetition of the data type and square brackets. But this is just for data types of int values, string, and char values. Otherwise, you need the new keyword. So you can do this: String[ ] aryStrings = {"Autumn", "Spring", "Summer", "Winter" };

To get at the values held in your array, you type the name of the array followed by an array position in square brackets. Like this: System.out.println( aryNums[2] ); The above code will print out whatever value is held at array position 2 in the array called aryNums. But let's get some coding practice.

Start a new project and call it anything you like. Don't forget to change the name of the Class to something relevant. Type the following code into your new Main method:

Create a new array Create a string array that will hold the 12 months of the year- Please assign the months to each element Create a integer array that will hold the dollar monetary values of money( 1, 5, 10….) Create an array called “toppings” and put in it at least 8 pizza toppings

Arrays with loops Code a program to print out the number of hours 3 employees worked (input from user) using an array

Loops & Arrays! imagine a lottery program that has to assign the numbers 1 to 49 to positions in an array. Instead of typing a long list of array positions and values you can use a loop. Length is a property of array objects that you can use to get the size of the array (how many positions it has). So this loop will keep going round and round while the value in the variable i is less than the size of the array.

To assign values to each position in the array, we have this line: lottery_numbers[i] = i + 1; Instead of a hard-code value between the square brackets of the array name, we have the variable called i. This increases by 1 each time round the loop, remember. Each array position can then be accessed just by using the loop value. The value that is being assigned to each position is i + 1. So again, it's just the incremented loop value, this time with 1 added to it. Because the loop value is starting at 0, this will give you the numbers 1 to 49. The other line in the loop just prints out whatever value is in each array position. (If you wanted, you could then write code to jumble up the numbers in the array. Once you have jumbled up the values, you could then take the first 6 and use them as the lottery numbers. Write another chunk of code that compares a user's 6 numbers with the winning numbers and you have a lottery program!)

Pizza Loop public class JavaStringArrayTests1 { private String[] toppings = {"Cheese", "Pepperoni", "Black Olives"}; // our constructor; print out the String array here public JavaStringArrayTests1() { // old `for` loop int size = toppings.length; for (int i=0; i<size; i++) { System.out.println(toppings[i]); } // main kicks everything off. // create a new instance of our class here. public static void main(String[] args) { new JavaStringArrayTests1(); }