Arrays.

Slides:



Advertisements
Similar presentations
Introduction to Arrays Chapter What is an array? An array is an ordered collection that stores many elements of the same type within one variable.
Advertisements

Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
Designing an ADT The design of an ADT should evolve naturally during the problem-solving process Questions to ask when designing an ADT What data does.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
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.
1.  Collections are data structures that holds data in different ways for flexible operations  C# Collection classes are defined as part of the ◦ System.Collections.
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.
Types in Java 8 Primitive Types –byte, short, int, long –float, double –boolean –Char Also some Object types: e.g. “String” But only single items. What.
1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
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.
Building java programs, chapter 3 Parameters, Methods and Objects.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
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];
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 10.
Chapter 8 Arrays and the ArrayList Class Arrays of Objects.
Arrays Chapter 7.
Insertion sort Loop invariants Dynamic memory
CMSC 202 ArrayList Aug 9, 2007.
Debugging with Eclipse
Chapter VII: Arrays.
Array in C# Array in C# RIHS Arshad Khan
Functions + Overloading + Scope
Test 2 Review Outline.
Arrays Low level collections.
AKA the birth, life, and death of variables.
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Chapter 7 Part 1 Edited by JJ Shepherd
Lecture-5 Arrays.
read the next character into ch using getchar();
Java Arrays. Array Object An array :a container object holds a fixed number of values of a single type. The length of an array is established when the.
Java Review: Reference Types
Advanced Programming Behnam Hatami Fall 2017.
Yong Choi School of Business CSU, Bakersfield
Object Oriented Programming COP3330 / CGS5409
Pass by Reference, const, readonly, struct
7 Arrays.
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.
CMSC 202 ArrayList Aug 9, 2007.
Java Programming Arrays
Arrays in Java What, why and how Copyright Curt Hill.
Parameter Passing in Java
CMSC 202 ArrayList Aug 9, 2007.
CS2011 Introduction to Programming I Arrays (II)
Topics discussed in this section:
CS2011 Introduction to Programming I Arrays (I)
AKA the birth, life, and death of variables.
int [] scores = new int [10];
Lecture 10: Arrays AP Computer Science Principles
Arrays.
Why did the programmer quit his job?
7 Arrays.
Dr Tripty Singh Arrays.
Object Oriented Programming
Arrays in Java.
Suggested self-checks: Section 7.11 #1-11
Data Structures & Algorithms
Arrays.
Lecture 6: References and linked nodes reading: 16.1
C++ Array 1.
1D Arrays and Lots of Brackets
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Arrays 3/4 June 3, 2019 ICS102: The course.
How do you do the following?
Review for Midterm 3.
SPL – PS2 C++ Memory Handling.
Week 7 - Monday CS 121.
Presentation transcript:

Arrays

An array is a collection of values of the same type stored consecutively in memory. int arr[] = new int[5]; This declaration has 2 parts: the stuff on the right of the assignment the stuff on the left Let’s start with the part on the right keyword new has two properties 1) allocates a chunk of memory big enough for 5 ints 2) brings back the address of that chunk of memory(or throw Exception if out of memory)

int arr[] = new int[5]; Produces this: arr: [0] [1] [2] [3] [4] The int arr[] part creates a reference variable named arr. This ref variable contains the address of the first cell in the memory chunk. arr contains the address of the [0]’th cell. That why there is an arrow coming out of arr pointing at the [0] cell. Thus arr points to or references the beginning of the array

Array Terminology arr is the reference variable The chunk of memory is the object References point to objects. Putting values into the array is easy Use .length to determine how many cells the array has for (int i=0 ; i<arr.length ; ++i ) arr[i] = i*2; now arr: [-]---> [0][2][4][6][8]

The .length property Once an array has been dimensioned you can always go back and ask the array how many cells of capacity it has. int arr[] = new int[5]; println( “arr has “ + arr.length + “ cells” ); This expression produces the number 5

You will use .length often and use count even more. Arrays and loops were made for each other. Here is how you use a loop and .length to fill an array. int arr[] = new int[5]; int count=0; while (count< arr.length ) { arr[count ++] = count *2; } 0 1 2 3 4 arr 0 2 4 6 8

The array “discipline” There are certain rules to follow in order to use an array correctly. We refer to these rules as the array discipline. Once you master these rules and become an advanced programmer you may find occasion to bend or violate them. In general however there are many good reasons for always following the rules.

The rules 1) When you declare an array you should declare an int named count or such to track how many values you have put into the array 2) initialize count to 0 and then use count to represent two things: the number of values you have put into the array so far the index position of where the next value should be stored

Passing Arrays public static void main( String[] args ) When you pass an array, you must pass it to a method that is written to receive and array. public static void main( String[] args ) { int arrCnt = 0; int arr[] = new int[5] for (arrCnt=0 ; arrCnt<arr.length ; arrCnt++) arr[arrCnt] = arrCnt * 2; printArray( arr, arrCnt); } private static void printArray( int[] array, int cnt ) { for (int i=0 ; i < cnt ; ++i System.out.println( array[i] ); System.out.println();

Passing Arrays II When you pass an array you are just passing a copy of the address where the array starts. You are not passing a copy of the data values. It would be very memory inefficient to make a copy of the actual array and send that to the method. It is much more efficient to just pass a copy of the address (reference)

Filling an Array from a file 45 23 56 21 76 13 int[] arr = new int[10]; int arrCnt= 0; while ( infile.hasNextInt() ) arr[arrCnt++] = infile.nextInt(); printArray( arr, arrCnt ); 0 1 2 3 4 5 6 7 8 9 arr 45 23 56 21 76 13 arrCnt is now 6. The last value is stored at arr[arrCnt-1]