Manipulating One-dimensional Arrays

Similar documents
AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

Arrays OBJECTIVES. In this chapter you will learn:

1. An operation in which an overall value is computed incrementally, often using a loop.

2.8. Decision Making: Equality and Relational Operators

Introduction to Computer Science Unit 2. Exercises

H212 Introduction to Software Systems Honors

Chapter 2: Basic Elements of Java

Arrays and Lists CSC 121 Fall 2016 Howard Rosenthal

Arrays. Eng. Mohammed Abdualal

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

COE 211/COE 212 Computer/Engineering Programming. Welcome to Exam II Thursday December 20, 2012

Arrays and Lists Review CSC 123 Fall 2018 Howard Rosenthal

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

5) (4 points) What is the value of the boolean variable equals after the following statement?

COMP 110 Programming Exercise: Simulation of the Game of Craps

Java Bootcamp - Villanova University. CSC 2014 Java Bootcamp. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Assignment Checklist. Prelab Activities. Lab Exercises. Labs Provided by Instructor. Postlab Activities. Section:

CMSC 150 INTRODUCTION TO COMPUTING LAB WEEK 3 STANDARD IO FORMATTING OUTPUT SCANNER REDIRECTING

Selection Statements and operators

Java Coding 3. Over & over again!

Chapter 3. Selections

Using Java Classes Fall 2018 Margaret Reid-Miller

CSCI Problem Solving, Programming and Computers Spring, 2016 Assignment 2 {Inheritance, Interfaces, Arrays}

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2009) - All Sections Midterm Examination

CS141 Programming Assignment #6

CS 1063 Introduction to Computer Programming Midterm Exam 2 Section 1 Sample Exam

download instant at

Selection Statements and operators

Java I/O and Control Structures

Faculty of Science Midterm. COMP-202B - Introduction to Computing I (Winter 2008)

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Topic 11 Scanner object, conditional execution

Programming with Java

Arrays. Here is the generic syntax for an array declaration: type[] <var_name>; Here's an example: int[] numbers;

Example: Monte Carlo Simulation 1

Controls Structure for Repetition

AP COMPUTER SCIENCE A

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: if Single-Selection Statement CSC 209 JAVA I. week 3- Control Statements: Part I

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

CSC1590 Java Assignment 2-0. User Defined Classes

Arrays and Lists CSC 121 Fall 2014 Howard Rosenthal

CompSci 125 Lecture 11

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

H212 Introduction to Software Systems Honors

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

A Quick and Dirty Overview of Java and. Java Programming

Algorithms and Conditionals

Java I/O and Control Structures Algorithms in everyday life

1 Short Answer (15 Points Each)

CSE 1223: Exam II Autumn 2016

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

CSC 1051 Data Structures and Algorithms I

NoSuchElementException 5. Name of the Exception that occurs when you try to read past the end of the input data in a file.

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Introduction to Java & Fundamental Data Types

CSC 1051 Algorithms and Data Structures I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Selection and Repetition Revisited

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

Data Structure and Programming Languages

CONTENTS: While loops Class (static) variables and constants Top Down Programming For loops Nested Loops

COMP 202. Programming With Iterations. CONTENT: The WHILE, DO and FOR Statements. COMP Loops 1

Oct Decision Structures cont d

private static final char[] Alphabet = "abcdefghijklmnopqrstuvwxyz".tochararray();

Choose 3 of the 1 st 4 questions (#'s 1 through 4) to complete. Each question is worth 12 points.

AP Computer Science Unit 1. Writing Programs Using BlueJ

CS212 Midterm. 1. Read the following code fragments and answer the questions.

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

Software Practice 1 - Basic Grammar Basic Syntax Data Type Loop Control Making Decision

CS 1083 Introduction to Programming I for Computer Scientists Final Solutions

Fundamentals of Programming Data Types & Methods

CSC 231 DYNAMIC PROGRAMMING HOMEWORK Find the optimal order, and its optimal cost, for evaluating the products A 1 A 2 A 3 A 4

Arrays. Weather Problem Array Declaration Accessing Elements Arrays and for Loops Array length field Quick Array Initialization Array Traversals

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Lecture 13 & 14. Single Dimensional Arrays. Dr. Martin O Connor CA166

Introduction to Arrays

Section 2.2 Your First Program in Java: Printing a Line of Text

Task #1 The if Statement, Comparing Strings, and Flags

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

Method OverLoading printf method Arrays Declaring and Using Arrays Arrays of Objects Array as Parameters

Installing Java. Tradition. DP Computer Science Unit 4.3: Intro to programming 1/17/2018. Software & websites

1 Short Answer (10 Points Each)

Software Practice 1 Basic Grammar

Building Java Programs

CS 112 Introduction to Programming

Arrays and Lists CSC 121 Fall 2015 Howard Rosenthal

Computational Expression

Midterm Examination (MTA)

Chapter 4: Control Structures I

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Chapter 3 Lab Decision Structures

Last Class. While loops Infinite loops Loop counters Iterations

Repetition CSC 121 Fall 2014 Howard Rosenthal

Full file at

CS 101 Spring 2007 Midterm 2 Name: ID:

CSC 1051 Villanova University. CSC 1051 Data Structures and Algorithms I. Course website:

Building Java Programs

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Midterm Examination

Give one example where you might wish to use a three dimensional array

Transcription:

Manipulating One-dimensional Arrays Mitsu Ogihara Department of Computer Science University of Miami 1 / 30

Table of Contents 1 For each 2 Exchanging Values 3 Reversing 2 / 30

For-each iteration For enumerating the elements of an array from the beginning to the end, one can use for each expression Suppose an array a is declared to be of <type>[], then for ( <type> <name> : a ) {... } retrieves the elements of a one after another and stores in the variable <name> For examples, if thelist is a String array with elements "Abraham", "Brigit", "Carlos", "Dwayne", and so on, for ( String xyz : arrayx ) { BODY } executes BODY with the value of xyz equal to "Abraham", equal to "Brigit", equal to "Carlos", equal to "Dwayne", and so on. This comes handy when performing a uniform action to the elements in the array in the order then appear 3 / 30

Benefits of For Each No need to speficy the index value since the elements are generated one after the other If the index value needs to be referred, better use the regular for-loop If s is an array of String objects, then for ( int i = 0; i < s.length; i++ ) { String w = s[i];... } is essentially the same as: int i = 0; for ( String w : s ) {... i++; } What are the differences? 4 / 30

String tochararray The method tochararray of class String returns an array of char that corresponds to the character sequence of the String For example, if w holds the value "CSC120" (excluding the double quote), then w.tochararray() returns the six element array [ C, S, C, 1, 2, 0 ] 5 / 30

Processing Letters in a String Scan the letters of a given input String and then converts any letter other than the apostrophe, the alphabet, and the digits to white space, but minimize the number of white space appearing between non-white space to 1 Use a boolean to remember whether the last char printed is a space Convert the string to a char-array and then process the elements using "for each" Given a char c at hand If c needs to be printed, print it and set the boolean to false Otherwise, print a white space if the boolean is false and then set the boolean to true 6 / 30

The Code: CharArray.java 1 import java.util.scanner; 2 public class CharArray { 3 public static void main(string[] args ) { 4 Scanner console = new Scanner(System.in); 5 System.out.print( "Enter a text line: " ); 6 String input = console.nextline(); 7 char[] carray = input.tochararray(); 8 boolean ispreviousspace = true; 9 for ( char c : carray ) { 10 if ( Character.isLowerCase( c ) 11 Character.isUpperCase( c ) 12 Character.isDigit( c ) c == \ ) { 13 System.out.print( c ); 14 ispreviousspace = false; 15 } 16 else if (!ispreviousspace ) { 17 System.out.print( " " ); 18 ispreviousspace = true; 19 } 20 } 21 System.out.println(); 22 } 23 } Create a scanner by the name of console and receive input from the user 7 / 30

The Code: CharArray.java 1 import java.util.scanner; 2 public class CharArray { 3 public static void main(string[] args ) { 4 Scanner console = new Scanner(System.in); 5 System.out.print( "Enter a text line: " ); 6 String input = console.nextline(); 7 char[] carray = input.tochararray(); 8 boolean ispreviousspace = true; 9 for ( char c : carray ) { 10 if ( Character.isLowerCase( c ) 11 Character.isUpperCase( c ) 12 Character.isDigit( c ) c == \ ) { 13 System.out.print( c ); 14 ispreviousspace = false; 15 } 16 else if (!ispreviousspace ) { 17 System.out.print( " " ); 18 ispreviousspace = true; 19 } 20 } 21 System.out.println(); 22 } 23 } Convert the input to a char array 7 / 30

The Code: CharArray.java 1 import java.util.scanner; 2 public class CharArray { 3 public static void main(string[] args ) { 4 Scanner console = new Scanner(System.in); 5 System.out.print( "Enter a text line: " ); 6 String input = console.nextline(); 7 char[] carray = input.tochararray(); 8 boolean ispreviousspace = true; 9 for ( char c : carray ) { 10 if ( Character.isLowerCase( c ) 11 Character.isUpperCase( c ) 12 Character.isDigit( c ) c == \ ) { 13 System.out.print( c ); 14 ispreviousspace = false; 15 } 16 else if (!ispreviousspace ) { 17 System.out.print( " " ); 18 ispreviousspace = true; 19 } 20 } 21 System.out.println(); 22 } 23 } Use for each on the array, the iterator variable is c 7 / 30

The Code: CharArray.java 1 import java.util.scanner; 2 public class CharArray { 3 public static void main(string[] args ) { 4 Scanner console = new Scanner(System.in); 5 System.out.print( "Enter a text line: " ); 6 String input = console.nextline(); 7 char[] carray = input.tochararray(); 8 boolean ispreviousspace = true; 9 for ( char c : carray ) { 10 if ( Character.isLowerCase( c ) 11 Character.isUpperCase( c ) 12 Character.isDigit( c ) c == \ ) { 13 System.out.print( c ); 14 ispreviousspace = false; 15 } 16 else if (!ispreviousspace ) { 17 System.out.print( " " ); 18 ispreviousspace = true; 19 } 20 } 21 System.out.println(); 22 } 23 } The case in which c needs to be printed no matter what 7 / 30

The Code: CharArray.java 1 import java.util.scanner; 2 public class CharArray { 3 public static void main(string[] args ) { 4 Scanner console = new Scanner(System.in); 5 System.out.print( "Enter a text line: " ); 6 String input = console.nextline(); 7 char[] carray = input.tochararray(); 8 boolean ispreviousspace = true; 9 for ( char c : carray ) { 10 if ( Character.isLowerCase( c ) 11 Character.isUpperCase( c ) 12 Character.isDigit( c ) c == \ ) { 13 System.out.print( c ); 14 ispreviousspace = false; 15 } 16 else if (!ispreviousspace ) { 17 System.out.print( " " ); 18 ispreviousspace = true; 19 } 20 } 21 System.out.println(); 22 } 23 } The remaining case; the else is not needed because the only action needed is to set the boolean to true, which is already is the case 7 / 30

The Code: CharArray.java 1 import java.util.scanner; 2 public class CharArray { 3 public static void main(string[] args ) { 4 Scanner console = new Scanner(System.in); 5 System.out.print( "Enter a text line: " ); 6 String input = console.nextline(); 7 char[] carray = input.tochararray(); 8 boolean ispreviousspace = true; 9 for ( char c : carray ) { 10 if ( Character.isLowerCase( c ) 11 Character.isUpperCase( c ) 12 Character.isDigit( c ) c == \ ) { 13 System.out.print( c ); 14 ispreviousspace = false; 15 } 16 else if (!ispreviousspace ) { 17 System.out.print( " " ); 18 ispreviousspace = true; 19 } 20 } 21 System.out.println(); 22 } 23 } Print a new line 7 / 30

Table of Contents 1 For each 2 Exchanging Values 3 Reversing 8 / 30

Exchanging Values Between Two Elements of an Array The problem: given an array X of TYPE and two indices p and q exchange values between the two elements X[p] and X[q] 9 / 30

Exchanging Values Between Two Elements of an Array The problem: given an array X of TYPE and two indices p and q exchange values between the two elements X[p] and X[q] A naive approach: execute X[p] = X[q]; X[q] = X[p]; What s wrong with this? 9 / 30

Exchanging Values Between Two Elements of an Array A correct generic approach: execute TYPE temporary = X[p]; X[p] = X[q]; X[q] = temporary; This method saves the value X[p] in a storage temporary 10 / 30

Graphical View words words words They They They say say say that that that falling love love in in in love love falling is is is wonderful. wonderful. wonderful. So So So temporary wonderful wonderful wonderful falling 11 / 30

Swapping Elements in an Array of String Objects Receive the user the number of elements Create a String array of that number of receive data from the user to store in the array Present the data with there position values Present the data with there position values Receive from the user two position values and perform exchange 12 / 30

Swapping Elements in an Array of Strings 1 import java.util. * ; 2 public class WordExchange { 3 public static void main( String[] args ) { 4 Scanner console = new Scanner( System.in ); 5 String[] thewords; 6 int size, pos, index1, index2; 7 String temporary; 8 //------ receive size 9 System.out.print( "How many words? " ); 10 size = Integer.parseInt( console.nextline() ); 11 //------ declare the array and receive words 12 thewords = new String[ size ]; 13 for ( pos = 0; pos < size; pos ++ ) { 14 System.out.printf( "Enter data %d: ", pos ); 15 thewords[ pos ] = console.nextline(); 16 } 17 //------ print the words 18 for ( pos = 0; pos < thewords.length; pos ++ ) { 19 System.out.printf( "The word no. %d is %s%n", 20 pos, thewords[ pos ] ); 21 } Ask the user to enter the size and create an array Use Integer.parseInt 13 / 30

Swapping Elements in an Array of Strings 1 import java.util. * ; 2 public class WordExchange { 3 public static void main( String[] args ) { 4 Scanner console = new Scanner( System.in ); 5 String[] thewords; 6 int size, pos, index1, index2; 7 String temporary; 8 //------ receive size 9 System.out.print( "How many words? " ); 10 size = Integer.parseInt( console.nextline() ); 11 //------ declare the array and receive words 12 thewords = new String[ size ]; 13 for ( pos = 0; pos < size; pos ++ ) { 14 System.out.printf( "Enter data %d: ", pos ); 15 thewords[ pos ] = console.nextline(); 16 } 17 //------ print the words 18 for ( pos = 0; pos < thewords.length; pos ++ ) { 19 System.out.printf( "The word no. %d is %s%n", 20 pos, thewords[ pos ] ); 21 } As before, interact with the user to receive the data 13 / 30

Swapping Elements in an Array of Strings 1 import java.util. * ; 2 public class WordExchange { 3 public static void main( String[] args ) { 4 Scanner console = new Scanner( System.in ); 5 String[] thewords; 6 int size, pos, index1, index2; 7 String temporary; 8 //------ receive size 9 System.out.print( "How many words? " ); 10 size = Integer.parseInt( console.nextline() ); 11 //------ declare the array and receive words 12 thewords = new String[ size ]; 13 for ( pos = 0; pos < size; pos ++ ) { 14 System.out.printf( "Enter data %d: ", pos ); 15 thewords[ pos ] = console.nextline(); 16 } 17 //------ print the words 18 for ( pos = 0; pos < thewords.length; pos ++ ) { 19 System.out.printf( "The word no. %d is %s%n", 20 pos, thewords[ pos ] ); 21 } Print the data 13 / 30

Swapping Elements in an Array of Strings 22 //------ perform exchange 23 System.out.print( "Enter index 1: " ); 24 index1 = console.nextint(); 25 System.out.print( "Enter index 2: " ); 26 index2 = console.nextint(); 27 if ( index1 >= 0 && index1 < size && 28 index2 >= 0 && index2 < size ) { 29 temporary = thewords[ index1 ]; 30 thewords[ index1 ] = thewords[ index2 ]; 31 thewords[ index2 ] = temporary; 32 } 33 System.out.println( "We have made the switch." ); 34 //------ print the words 35 for ( pos = 0; pos < thewords.length; pos ++ ) { 36 System.out.printf( "The word no. %d is %s%n", 37 pos, thewords[ pos ] ); 38 } 39 } 40 } Receive indices; use Integer.parseInt 14 / 30

Swapping Elements in an Array of Strings 22 //------ perform exchange 23 System.out.print( "Enter index 1: " ); 24 index1 = console.nextint(); 25 System.out.print( "Enter index 2: " ); 26 index2 = console.nextint(); 27 if ( index1 >= 0 && index1 < size && 28 index2 >= 0 && index2 < size ) { 29 temporary = thewords[ index1 ]; 30 thewords[ index1 ] = thewords[ index2 ]; 31 thewords[ index2 ] = temporary; 32 } 33 System.out.println( "We have made the switch." ); 34 //------ print the words 35 for ( pos = 0; pos < thewords.length; pos ++ ) { 36 System.out.printf( "The word no. %d is %s%n", 37 pos, thewords[ pos ] ); 38 } 39 } 40 } Exchanging elements between two indices entered Do this only if they are valid indices 14 / 30

Swapping Elements in an Array of Strings 22 //------ perform exchange 23 System.out.print( "Enter index 1: " ); 24 index1 = console.nextint(); 25 System.out.print( "Enter index 2: " ); 26 index2 = console.nextint(); 27 if ( index1 >= 0 && index1 < size && 28 index2 >= 0 && index2 < size ) { 29 temporary = thewords[ index1 ]; 30 thewords[ index1 ] = thewords[ index2 ]; 31 thewords[ index2 ] = temporary; 32 } 33 System.out.println( "We have made the switch." ); 34 //------ print the words 35 for ( pos = 0; pos < thewords.length; pos ++ ) { 36 System.out.printf( "The word no. %d is %s%n", 37 pos, thewords[ pos ] ); 38 } 39 } 40 } Print the data 14 / 30

Table of Contents 1 For each 2 Exchanging Values 3 Reversing 15 / 30

Reversing an Array Suppose we have an array of integers in which the elements appear in the ascending order. Suppose we want to reverse the order. How do you do it? 16 / 30

Solution No.1 : Creating a new array Create a new array of the same type and the same dimension Copy the elements from the original to this new array in the reverse order Rename the new array with the original name 17 / 30

Solution No.1 : Creating a new array Create a new array of the same type and the same dimension Copy the elements from the original to this new array in the reverse order Rename the new array with the original name TYPE[] SUBSTITUTE = new TYPE[OLD.length]; for (int index = 0; index < OLD.length; index ++) { SUBSTITUTE[index] = OLD[OLD.length - 1 - index]; } OLD = SUBSITUTE; 17 / 30

Solution No. 2: Reversing in place Exchange between element 0 and element LENGTH - 1 Exchange between element 1 and element LENGTH - 2 Exchange between element 2 and element LENGTH - 3... Exchange between element M and element LENGTH - 1 - M 18 / 30

Solution No. 2: Reversing in place... 0 1 2 3 LENGTH-3 LENGTH-1 LENGTH-4 LENGTH-2 19 / 30

Solution No. 2: Reversing in place Exchange between element 0 and element LENGTH - 1 Exchange between element 1 and element LENGTH - 2 Exchange between element 2 and element LENGTH - 3... Exchange between element M and element LENGTH - 1 - M 20 / 30

Solution No. 2: Reversing in place Exchange between element 0 and element LENGTH - 1 Exchange between element 1 and element LENGTH - 2 Exchange between element 2 and element LENGTH - 3... Exchange between element M and element LENGTH - 1 - M What is the right value for M? 20 / 30

Solution No. 2: Reversing in place Exchange between element 0 and element LENGTH - 1 Exchange between element 1 and element LENGTH - 2 Exchange between element 2 and element LENGTH - 3... Exchange between element M and element LENGTH - 1 - M What is the right value for M? M = LENGTH/2-1 20 / 30

Demonstration using an Eleven Element Array 1 public class ReverseByExchange { 2 public static void print( int[] data ) { 3 for ( int i = 0; i < data.length; i ++ ) { 4 System.out.print( data[ i ] ); 5 if ( i < data.length - 1 ) { 6 System.out.print( " " ); 7 } 8 } 9 System.out.println(); 10 } 11 public static void main( String[] args ) { 12 int[] numbers = new int[ 11 ]; 13 for ( int index = 0; index < 11; index ++ ) { 14 numbers[ index ] = index; 15 } 16 print( numbers ); 17 for ( int index = 0; index < numbers.length / 2; index ++ ) { 18 int temporary = numbers[ index ]; 19 numbers[ index ] = numbers[ numbers.length - 1- index ]; 20 numbers[ numbers.length - 1 - index ] = temporary; 21 } 22 print( numbers ); 23 } 24 } A method for printing an int array 21 / 30

Demonstration using an Eleven Element Array 1 public class ReverseByExchange { 2 public static void print( int[] data ) { 3 for ( int i = 0; i < data.length; i ++ ) { 4 System.out.print( data[ i ] ); 5 if ( i < data.length - 1 ) { 6 System.out.print( " " ); 7 } 8 } 9 System.out.println(); 10 } 11 public static void main( String[] args ) { 12 int[] numbers = new int[ 11 ]; 13 for ( int index = 0; index < 11; index ++ ) { 14 numbers[ index ] = index; 15 } 16 print( numbers ); 17 for ( int index = 0; index < numbers.length / 2; index ++ ) { 18 int temporary = numbers[ index ]; 19 numbers[ index ] = numbers[ numbers.length - 1- index ]; 20 numbers[ numbers.length - 1 - index ] = temporary; 21 } 22 print( numbers ); 23 } 24 } Print all elements in one line with one white space in between and go to newline at the end 21 / 30

Demonstration using an Eleven Element Array 1 public class ReverseByExchange { 2 public static void print( int[] data ) { 3 for ( int i = 0; i < data.length; i ++ ) { 4 System.out.print( data[ i ] ); 5 if ( i < data.length - 1 ) { 6 System.out.print( " " ); 7 } 8 } 9 System.out.println(); 10 } 11 public static void main( String[] args ) { 12 int[] numbers = new int[ 11 ]; 13 for ( int index = 0; index < 11; index ++ ) { 14 numbers[ index ] = index; 15 } 16 print( numbers ); 17 for ( int index = 0; index < numbers.length / 2; index ++ ) { 18 int temporary = numbers[ index ]; 19 numbers[ index ] = numbers[ numbers.length - 1- index ]; 20 numbers[ numbers.length - 1 - index ] = temporary; 21 } 22 print( numbers ); 23 } 24 } Create an eleven element array 21 / 30

Demonstration using an Eleven Element Array 1 public class ReverseByExchange { 2 public static void print( int[] data ) { 3 for ( int i = 0; i < data.length; i ++ ) { 4 System.out.print( data[ i ] ); 5 if ( i < data.length - 1 ) { 6 System.out.print( " " ); 7 } 8 } 9 System.out.println(); 10 } 11 public static void main( String[] args ) { 12 int[] numbers = new int[ 11 ]; 13 for ( int index = 0; index < 11; index ++ ) { 14 numbers[ index ] = index; 15 } 16 print( numbers ); 17 for ( int index = 0; index < numbers.length / 2; index ++ ) { 18 int temporary = numbers[ index ]; 19 numbers[ index ] = numbers[ numbers.length - 1- index ]; 20 numbers[ numbers.length - 1 - index ] = temporary; 21 } 22 print( numbers ); 23 } 24 } Store index to slot index for all index=0 to 10 21 / 30

Demonstration using an Eleven Element Array 1 public class ReverseByExchange { 2 public static void print( int[] data ) { 3 for ( int i = 0; i < data.length; i ++ ) { 4 System.out.print( data[ i ] ); 5 if ( i < data.length - 1 ) { 6 System.out.print( " " ); 7 } 8 } 9 System.out.println(); 10 } 11 public static void main( String[] args ) { 12 int[] numbers = new int[ 11 ]; 13 for ( int index = 0; index < 11; index ++ ) { 14 numbers[ index ] = index; 15 } 16 print( numbers ); 17 for ( int index = 0; index < numbers.length / 2; index ++ ) { 18 int temporary = numbers[ index ]; 19 numbers[ index ] = numbers[ numbers.length - 1- index ]; 20 numbers[ numbers.length - 1 - index ] = temporary; 21 } 22 print( numbers ); 23 } 24 } Print the elements 21 / 30

Demonstration using an Eleven Element Array 1 public class ReverseByExchange { 2 public static void print( int[] data ) { 3 for ( int i = 0; i < data.length; i ++ ) { 4 System.out.print( data[ i ] ); 5 if ( i < data.length - 1 ) { 6 System.out.print( " " ); 7 } 8 } 9 System.out.println(); 10 } 11 public static void main( String[] args ) { 12 int[] numbers = new int[ 11 ]; 13 for ( int index = 0; index < 11; index ++ ) { 14 numbers[ index ] = index; 15 } 16 print( numbers ); 17 for ( int index = 0; index < numbers.length / 2; index ++ ) { 18 int temporary = numbers[ index ]; 19 numbers[ index ] = numbers[ numbers.length - 1- index ]; 20 numbers[ numbers.length - 1 - index ] = temporary; 21 } 22 print( numbers ); 23 } 24 } The range of index for the reversal is 0 through numbers.length/2-1 21 / 30

Demonstration using an Eleven Element Array 1 public class ReverseByExchange { 2 public static void print( int[] data ) { 3 for ( int i = 0; i < data.length; i ++ ) { 4 System.out.print( data[ i ] ); 5 if ( i < data.length - 1 ) { 6 System.out.print( " " ); 7 } 8 } 9 System.out.println(); 10 } 11 public static void main( String[] args ) { 12 int[] numbers = new int[ 11 ]; 13 for ( int index = 0; index < 11; index ++ ) { 14 numbers[ index ] = index; 15 } 16 print( numbers ); 17 for ( int index = 0; index < numbers.length / 2; index ++ ) { 18 int temporary = numbers[ index ]; 19 numbers[ index ] = numbers[ numbers.length - 1- index ]; 20 numbers[ numbers.length - 1 - index ] = temporary; 21 } 22 print( numbers ); 23 } 24 } Exchange between two elements using temporary as a temporary storage 21 / 30

Demonstration using an Eleven Element Array 1 public class ReverseByExchange { 2 public static void print( int[] data ) { 3 for ( int i = 0; i < data.length; i ++ ) { 4 System.out.print( data[ i ] ); 5 if ( i < data.length - 1 ) { 6 System.out.print( " " ); 7 } 8 } 9 System.out.println(); 10 } 11 public static void main( String[] args ) { 12 int[] numbers = new int[ 11 ]; 13 for ( int index = 0; index < 11; index ++ ) { 14 numbers[ index ] = index; 15 } 16 print( numbers ); 17 for ( int index = 0; index < numbers.length / 2; index ++ ) { 18 int temporary = numbers[ index ]; 19 numbers[ index ] = numbers[ numbers.length - 1- index ]; 20 numbers[ numbers.length - 1 - index ] = temporary; 21 } 22 print( numbers ); 23 } 24 } Print the elements 21 / 30

Adding an Element to an Array Suppose you want to add an element to an existing array at a particular location. How do you do it? 22 / 30

General Solution for Insertion Create a new temporary array with a different name, say newarray Copy all the elements before the insertion point at the same element location Add the new element at the designated location Copy all the elements on and after the inserttion point to one higher position Set the current array to the temporary 23 / 30

Graphical View index new element oldone newone 24 / 30

General Solution for Deletion Create a new temporary array with a different name, say newarray Copy all the elements before the removal point at the same element location Copy all the elements on and after the inserttion point to one lower position Set the current array to the temporary 25 / 30

Graphical View index oldone newone 26 / 30

The Code: InsertAndDelete.java 1 public class InsertAndDelete { 2 //--- print the elements of a given array 3 public static void print( String[] names ) { 4 for ( String w : names ) { 5 System.out.print( " " + w ); 6 } 7 System.out.println(); 8 } Method that prints the elements of an array 27 / 30

The Code: InsertAndDelete.java 1 public class InsertAndDelete { 2 //--- print the elements of a given array 3 public static void print( String[] names ) { 4 for ( String w : names ) { 5 System.out.print( " " + w ); 6 } 7 System.out.println(); 8 } Using for each and prints each element with a " " in front 27 / 30

The Code: InsertAndDelete.java (insert) 9 //--- main 10 public static void main(string[] args) { 11 String[] names = new String[]{ 12 "Sarah", "Peggy", "Helen", "Dinah", "Ella", "Billie" }; 13 print( names ); 14 //------- removing element 3 15 String[] namesless = new String[ names.length - 1 ]; 16 for ( int i = 0; i <= 2; i ++ ) { 17 namesless[i] = names[i]; 18 } 19 for ( int i = 3; i < names.length; i ++ ) { 20 namesless[i-1] = names[i]; 21 } 22 print( namesless ); The names array; print the array contents using the method print 28 / 30

The Code: InsertAndDelete.java (insert) 9 //--- main 10 public static void main(string[] args) { 11 String[] names = new String[]{ 12 "Sarah", "Peggy", "Helen", "Dinah", "Ella", "Billie" }; 13 print( names ); 14 //------- removing element 3 15 String[] namesless = new String[ names.length - 1 ]; 16 for ( int i = 0; i <= 2; i ++ ) { 17 namesless[i] = names[i]; 18 } 19 for ( int i = 3; i < names.length; i ++ ) { 20 namesless[i-1] = names[i]; 21 } 22 print( namesless ); Create a new array with one fewer elements 28 / 30

The Code: InsertAndDelete.java (insert) 9 //--- main 10 public static void main(string[] args) { 11 String[] names = new String[]{ 12 "Sarah", "Peggy", "Helen", "Dinah", "Ella", "Billie" }; 13 print( names ); 14 //------- removing element 3 15 String[] namesless = new String[ names.length - 1 ]; 16 for ( int i = 0; i <= 2; i ++ ) { 17 namesless[i] = names[i]; 18 } 19 for ( int i = 3; i < names.length; i ++ ) { 20 namesless[i-1] = names[i]; 21 } 22 print( namesless ); Copy elements before the point of elimination 28 / 30

The Code: InsertAndDelete.java (insert) 9 //--- main 10 public static void main(string[] args) { 11 String[] names = new String[]{ 12 "Sarah", "Peggy", "Helen", "Dinah", "Ella", "Billie" }; 13 print( names ); 14 //------- removing element 3 15 String[] namesless = new String[ names.length - 1 ]; 16 for ( int i = 0; i <= 2; i ++ ) { 17 namesless[i] = names[i]; 18 } 19 for ( int i = 3; i < names.length; i ++ ) { 20 namesless[i-1] = names[i]; 21 } 22 print( namesless ); Copy the rest to one lower position 28 / 30

The Code: InsertAndDelete.java (insert) 9 //--- main 10 public static void main(string[] args) { 11 String[] names = new String[]{ 12 "Sarah", "Peggy", "Helen", "Dinah", "Ella", "Billie" }; 13 print( names ); 14 //------- removing element 3 15 String[] namesless = new String[ names.length - 1 ]; 16 for ( int i = 0; i <= 2; i ++ ) { 17 namesless[i] = names[i]; 18 } 19 for ( int i = 3; i < names.length; i ++ ) { 20 namesless[i-1] = names[i]; 21 } 22 print( namesless ); Print 28 / 30

The Code: InsertAndDelete.java (delete) 23 //------- adding "Nancy" as element 4 24 String[] namesmore = new String[ names.length + 1 ]; 25 for ( int i = 0; i <= 3; i ++ ) { 26 namesmore[i] = names[i]; 27 } 28 namesmore[4] = "Nancy"; 29 for ( int i = 4; i < names.length; i ++ ) { 30 namesmore[i+1] = names[i]; 31 } 32 print( namesmore ); 33 } 34 } Create a new array with one more elements 29 / 30

The Code: InsertAndDelete.java (delete) 23 //------- adding "Nancy" as element 4 24 String[] namesmore = new String[ names.length + 1 ]; 25 for ( int i = 0; i <= 3; i ++ ) { 26 namesmore[i] = names[i]; 27 } 28 namesmore[4] = "Nancy"; 29 for ( int i = 4; i < names.length; i ++ ) { 30 namesmore[i+1] = names[i]; 31 } 32 print( namesmore ); 33 } 34 } Copy elements before the point of insertion 29 / 30

The Code: InsertAndDelete.java (delete) 23 //------- adding "Nancy" as element 4 24 String[] namesmore = new String[ names.length + 1 ]; 25 for ( int i = 0; i <= 3; i ++ ) { 26 namesmore[i] = names[i]; 27 } 28 namesmore[4] = "Nancy"; 29 for ( int i = 4; i < names.length; i ++ ) { 30 namesmore[i+1] = names[i]; 31 } 32 print( namesmore ); 33 } 34 } Place the new element 29 / 30

The Code: InsertAndDelete.java (delete) 23 //------- adding "Nancy" as element 4 24 String[] namesmore = new String[ names.length + 1 ]; 25 for ( int i = 0; i <= 3; i ++ ) { 26 namesmore[i] = names[i]; 27 } 28 namesmore[4] = "Nancy"; 29 for ( int i = 4; i < names.length; i ++ ) { 30 namesmore[i+1] = names[i]; 31 } 32 print( namesmore ); 33 } 34 } Copy the rest to one higher position 29 / 30

The Code: InsertAndDelete.java (delete) 23 //------- adding "Nancy" as element 4 24 String[] namesmore = new String[ names.length + 1 ]; 25 for ( int i = 0; i <= 3; i ++ ) { 26 namesmore[i] = names[i]; 27 } 28 namesmore[4] = "Nancy"; 29 for ( int i = 4; i < names.length; i ++ ) { 30 namesmore[i+1] = names[i]; 31 } 32 print( namesmore ); 33 } 34 } Print 29 / 30

The End 30 / 30