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.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Arrays. Topics Tables of Data Arrays – Single Dimensional Parsing a String into Multiple Tokens Arrays - Multi-dimensional.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
11-1 Chapter 11 2D Arrays Asserting Java Rick Mercer.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
©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.
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.
Arrays. Example Write a program to keep track of all students’ scores on exam 1. Need a list of everyone’s score Declare 14 double variables? What about.
Array Must declare a variable to reference the array double [] mylist; // cannot double list[20]; Or double mylist[]; The declaration doesn’t allocate.
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Arrays (Part II). Two- and Multidimensional Arrays Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two.
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
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.
Java Unit 9: Arrays Declaring and Processing Arrays.
One Dimensional Arrays. Declaring references to array objects How would you declare a variable somearray that is an array of ints? int[] somearray;
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
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.
AP Comp Sci A Chapter 12 - Arrays. Ch 12 Goals: Goals: Declare and create arrays Declare and create arrays Access elements in arrays Access elements in.
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.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
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.
CHAPTER: 12. Array is a collection of variables of the same data type that are referenced by a common name. An Array of 10 Elements of type double.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Two –Dimensional Arrays Mrs. C. Furman September 18, 2008.
Scope When we create variables and functions, they are limited in where they are visible and where they can be referenced For the most part, the identifiers.
Data Structure CS 322. What is an array? Initializing arrays Accessing the values of an array Multidimensional arrays LAB#1 : Arrays.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/19/20151.
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)
Operator precedence.  Evaluate a + b * c –multiplication first? a + (b * c) –addition first? ( a + b) * c  Java solves this problem by assigning priorities.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
Arrays.
Computer Programming for Engineers
1. Define an array 1 Create reference arrays of objects in Java program 2 Initialize elements of arrays 3 Pass array to methods 4 Return array to methods.
Arrays.
Module 1: Array ITEI222 - Advance Programming Language.
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];
 Introducing Arrays  Declaring Array Variables, Creating Arrays, and Initializing Arrays  Copying Arrays  Multidimensional Arrays  Search and Sorting.
Visual C# 2005 Using Arrays. Visual C# Objectives Declare an array and assign values to array elements Initialize an array Use subscripts to access.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
1.8 Multidimensional Arrays academy.zariba.com 1.
Dr. Sajib Datta Feb 11,  Example of declaring and initializing an array. ◦ double someData[3]; /* declare the array someData that will.
ARRAYS Multidimensional realities Image courtesy of
For Friday Read No quiz Program 6 due. Program 6 Any questions?
© 2004 Pearson Addison-Wesley. All rights reserved7-1 Array review Array of primitives int [] count; count = new int[10]; Array of objects Grade [] cs239;
Arrays. Arrays are objects that help us organize large amounts of information.
 2005 Pearson Education, Inc. All rights reserved Arrays.
Asserting Java © Rick Mercer Chapter 7 The Java Array Object.
© Rick Mercer Chapter 7 The Java Array Object.  Some variables store precisely one value: a double stores one floating-point number a double stores one.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
Two-Dimensional Data Class of 5 students Each student has 3 test scores Store this information in a two- dimensional array First dimension: which student.
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;
Dr. Sajib Datta Sep 10,  #include  void main()  {  int a = 25;  int b = 0;  int c = -35;  if( a || b ) ◦ printf("Test1\n");  else.
Chapter 6: Using Arrays.
Write code to prompt for 5 grades, read them in, print “Thank you”, then reprint the 5 grades and their average. System.out.println(“Please enter grade.
Computer Programming BCT 1113
Two Dimensional Array Mr. Jacobs.
Write code to prompt for 5 grades, read them in, print “Thank you”, then reprint the 5 grades and their average. cout >
Introduction To Programming Information Technology , 1’st Semester
Multidimensional Arrays
MSIS 655 Advanced Business Applications Programming
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays
Multidimensional array
Presentation transcript:

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 each one out Collections are data types that hold groups of data

Arrays An array is a data type that keeps data in a line. All the elements in the array have to be the same type. Arrays aren’t primitives, but their syntax isn’t really like objects

Array Indices Elements in an array are numbered for convenience Numbering starts at 0, like Strings

Syntax for Arrays Declare an array: type of data in the array, followed by square brackets. int[ ] numbers; Car[ ] myCars; Create an array: use word new and the number of elements in the array numbers = new int[7]; myCars = new Car[5]; Can combine the two statements: Car [ ] myCars = new Car[5]; Notice there are no parentheses!!!

Array Elements Use square brackets to access individual array elements int x = myNums[4] means put the number in index 4 into variable x. Shortcut for filling up an array: double[ ] myList = {1.9, 3.4, -3.2, 0.0}; Using this way you don’t need the word new.

Array Elements Also put things in an array with element notation: myNumbers[4] = 5; Access the length of any array with.length int len = myNumbers.length No parentheses!!!

Examples Make an array of 100 integers, where each number in the array is equal to twice its index value Sum up all numbers in an array Write a method that reverses an array. Find the middle number in an array of ints Find the biggest number in an array of ints

Objects in Arrays Arrays are capable of holding primitive as well as object types. Creating an array of objects does not create the objects in the array. Sometimes the length of the array is not the same as the number of elements in the array that you want to use.

Visualizing an Array of Objects Create the array Car[ ] myCars = new Car[5]; Create the objects for(int i = 0; i< myCars.length; i++){ myCars[i] = new Car(); } Ø Ø Ø Ø Ø myCars

Passing Arrays to Methods Arrays can be input or output to methods just like any other data type Arrays are passed to methods by reference, like objects Arrays must be copied explicitly int[ ] nums1 = {1,2,3}; int[ ] nums2 = nums1; nums1[1] = 5; System.out.println(nums1[1]); System.out.println(nums2[1]);

More Exercises Create an array of 50 BankAccounts. Then deposit $100 in each. Take an array of Car objects and return the Car with the lowest gas mileage. (Assume there is a getGasMileage() method). public Car lowestGasMileage(Car[] myCars) Take an array of Students and return the student object whose name is given. Assume there is a getName() method. public Student hasName(Student[] students, String name)

Multidimensional Arrays We can also create arrays of arrays (matrices) int[ ][ ] matrix = new int[2][4];

Initializing Shortcut int[ ][ ] matrix = { {0,3,2,4},{8,9,2,1} };

Multidimensional Arrays myNumbers[1][2] = 1 myNumbers.length is the number of rows myNumbers[0] is the first row myNumbers[0].length is the number of elements in the first row (same as number of columns) myNumbers

Array Patterns Access every element in an array: for(int i = 0; i<array.length; i++){ int x = array[i]; } Access every element in a matrix: for(int i = 0; i<array.length; i++){ for(int j = 0; j< array[i].length; j++){ int x = array[i][j]; } }

Exercises Create a multiplication table (mult[a][b] = axb) public int[][] multTable(int a, int b){ Add all the elements in a table public int addUp(int[][]myTable){ Print the indices of a number in the table public void findIndices(int target, int[][] table){