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.

Slides:



Advertisements
Similar presentations
Arrays An array is a structure that holds multiple values of the same type. The length of an array is established when the array is created (at runtime).
Advertisements

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.
Array Yoshi. Definition An array is a container object that holds a fixed number of values of a single type. The length of an array is established when.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
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.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Java Unit 9: Arrays Declaring and Processing Arrays.
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
03/16/ What is an Array?... An array is an object that stores list of items. Each slot of an array holds an individual element. Characteristics.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
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.
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.
Chapter 8: Arrays.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Lecture 5: Arrays A way to organize data MIT AITI April 9th, 2005.
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.
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.
August 6, 2009 Data Types, Variables, and Arrays.
Lecture4: Arrays Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
IT259 Foundation of Programming Using Java Unit 9 Seminar : (Chapter 8 ) Instructor : Vladimir Gubanov, PhD
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
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];
Arrays in java Unit-1 Introduction to Java. Array There are situations where we might wish to store a group of similar type of values in a variable. Array.
Array and String.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Java Programming Language Lecture27- An Introduction.
Lecture 5 array declaration and instantiation array reference
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter VII: Arrays.
Array in C# Array in C# RIHS Arshad Khan
Computer Organization and Design Pointers, Arrays and Strings in C
Arrays 3/4 By Pius Nyaanga.
Selenium WebDriver Web Test Tool Training
A simple way to organize data
Object Oriented Programming COP3330 / CGS5409
Type Conversion, Constants, and the String Object
Pass by Reference, const, readonly, struct
An Introduction to Java – Part I, language basics
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.
EKT150 : Computer Programming
Introduction To Programming Information Technology , 1’st Semester
Defining methods and more arrays
Chapter 2: Java Fundamentals
BIT115: Introduction to Programming
CS2011 Introduction to Programming I Arrays (I)
MSIS 655 Advanced Business Applications Programming
Multidimensional array
Single-Dimensional Arrays chapter6
Arrays in Java.
Suggested self-checks: Section 7.11 #1-11
Java SE 7 One and Multi Dimensional Arrays Module 6
Arrays Arrays A few types Structures of related data items
Java Programming Language
Arrays in Java Prepare 1 whole yellow paper.
Java’s Central Casting
C++ Array 1.
Arrays 3/4 June 3, 2019 ICS102: The course.
Chapter 6 Arrays.
How do you do the following?
Introduction to java Part I By Shenglan Zhang.
Classes and Objects Object Creation
Arrays.
Presentation transcript:

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 array is created. After creation, its length is fixed.

Array element and indexing Each item in an array: called an element each element is accessed by its numerical index. Numbering begins with 0. The 9th element, for example, would therefore be accessed at index 8.

ArrayDemo Class ArrayDemo { public static void main(String[] args) { // declares an array of integers int[] anArray; // allocates memory for 10 integers anArray = new int[10]; // initialize first element anArray[0] = 100; // initialize second element anArray[1] = 200; // and so forth anArray[9] = 1000; System.out.println("Element at index 0: " + anArray[0]); //Similarly elements at 1 –9 }

Declaring Variable to Refer to an Array Declaring a Variable to Refer to an Array : // declares an array of integers int[] anArray; array declaration has two components: the array's type and the array's name. An array's type is written as type[], where type is the data type of the contained elements ; ( the brackets are special symbols indicating that this variable holds an array ) The size of the array is not part of its type (which is why the brackets are empty). An array's name can be anything you want, provided that it follows the rules and conventions in the naming section. As with variables of other types, the declaration does not actually create an array; it simply tells the compiler that this variable will hold an array of specified type.naming

Other array types byte[] anArrayOfBytes; short[] anArrayOfShorts; long[] anArrayOfLongs; float[] anArrayOfFloats; double[] anArrayOfDoubles; boolean[] anArrayOfBooleans; char[] anArrayOfChars; String[] anArrayOfStrings; You can also place the brackets after the array's name: // this form is discouraged float anArrayOfFloats[];

Creating, Initializing, and Accessing an Array One way to create an array is with the new operator. The next statement in the ArrayDemo program allocates an array with enough memory for 10 integer elements and assigns the array to the anArray variable. // create an array of integers anArray = new int[10]; If this statement is missing, then compiler prints an error like the following, and compilation fails: ArrayDemo.java:4: Variable anArray may not have been initialized

Alternate method of initialization The length of the array is determined by the number of values provided between braces and separated by commas.

You can also declare an array of arrays (also known as a multidimensional array) by using two or more sets of brackets, such as String[][] names. Each element, therefore, must be accessed by a corresponding number of index values. MultiDimensional Arrays

Length property of array you can use the built-in length property to determine the size of any array. The following code prints the array's size to standard output : System,out,println(anArray.length); System.out.println(anArray.length);

Array Copy

copyOfRange the previous example can be modified to use the copyOfRange method of the java.util.Arrays class, as you can see in the ArrayCopyOfDemo example. ArrayCopyOfDemo The difference is that using the copyOfRange method does not require you to create the destination array before calling the method, the destination array is returned by the method:

Useful operations of java.util.Arrays class

Anonymous arrray smallPrimes = new int[] { 17, 19, 23,29,31,37};

String args[] array

Passing Java Array to method

Matrix Multiplication

Matrix Multiplication… code/java-program-multiply-two-matrices

Sorting using Java Arrays Java 2D Arrays

Bubble Sort public class BubbleSortExample { static void bubbleSort(int[] arr) { int n = arr.length; int temp = 0; for(int i=0; i < n; i++){ for(int j=1; j < (n-i); j++){ if(arr[j-1] > arr[j]){ //swap elements temp = arr[j-1]; arr[j-1] = arr[j]; arr[j] = temp; } } } }

Bubble Sort… public static void main(String[] args) { int arr[] ={3,60,35,2,45,320,5}; System.out.println("Array Before Bubble Sort"); for(int i=0; i < arr.length; i++){ System.out.print(arr[i] + " "); } System.out.println(); bubbleSort(arr);//sorting array elements using bubble sort System.out.println("Array After Bubble Sort"); for(int i=0; i < arr.length; i++){ System.out.print(arr[i] + " "); }

Sorting Algos Selection Sort Bubble Sort Merge Sort

2D Array Sorting

Arrays.sort() method // A sample Java program to sort an array of integers // using Arrays.sort(). It by default sorts in // ascending order import java.util.Arrays; public class SortExample { public static void main(String[] args) { // Our arr contains 8 elements int[] arr = {13, 7, 6, 45, 21, 9, 101, 102}; Arrays.sort(arr); System.out.printf("Modified arr[] : %s", Arrays.toString(arr)); } Modified arr[] : [6, 7, 9, 13, 21, 45, 101, 102] A Java program to sort an array of integers in ascending order. Output: Modified arr[] : [6, 7, 9, 13, 21, 45, 101, 102]

Arrays.sort() method // A sample Java program to sort an array of integers // using Arrays.sort(). It by default sorts in // ascending order import java.util.Arrays; public class SortExample { public static void main(String[] args) { // Our arr contains 8 elements int[] arr = {13, 7, 6, 45, 21, 9, 101, 102}; Arrays.sort(arr); System.out.printf("Modified arr[] : %s", Arrays.toString(arr)); } Modified arr[] : [6, 7, 9, 13, 21, 45, 101, 102] A Java program to sort an array of integers in ascending order. Output: Modified arr[] : [6, 7, 9, 13, 21, 45, 101, 102]

Sorting subarray import java.util.Arrays; public class SortExample { public static void main(String[] args) { // Our arr contains 8 elements int[] arr = {13, 7, 6, 45, 21, 9, 2, 100}; // Sort subarray from index 1 to 5, i.e., // only sort subarray {7, 6, 45, 21} and // keep other elements as it is. Arrays.sort(arr, 1, 5); System.out.printf("Modified arr[] : %s", Arrays.toString(arr)); } Modified arr[] : [13, 6, 7, 21, 45, 9, 2, 100] Output:

Descending Order Sort import java.util.Arrays; import java.util.Collections; public class SortExample { public static void main(String[] args) { // Note that we have Integer here instead of // int[] as Collections.reverseOrder doesn't // work for primitive types. Integer[] arr = {13, 7, 6, 45, 21, 9, 2, 100}; // Sorts arr[] in descending order Arrays.sort(arr, Collections.reverseOrder()); System.out.printf("Modified arr[] : %s", Arrays.toString(arr)); }