arrays useful methods

29
Java.util.Arrays class

Upload: knox-abbott

Post on 03-Jan-2016

35 views

Category:

Documents


2 download

DESCRIPTION

Java.util.Arrays class. Arrays Useful methods. Use of ready methods to manipulate arrays. java.lang.System class java.util.Arrays class. Java Arrays – Copying arraycopy(). The class java.lang.System contains a method arraycopy that copies array efficiently. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Arrays Useful methods

Java.util.Arrays class

Page 2: Arrays Useful methods

java.lang.System class java.util.Arrays class

Page 3: Arrays Useful methods

The class java.lang.System contains a method arraycopy that copies array efficiently.

int array1[] = new int[10]; int array2[] = new int[10]; //copy array1 into array2 System.arraycopy(array1, 0, array2, 0, 10); //copy last 5 elements in array1 into first 5 of array2 System.arraycopy(array1, 5, array2, 0, 5);

Page 4: Arrays Useful methods

arraycopy(Object src, int srcPos, Object dest, int destPos, int length)

class CopyArray{

public static void main(String [] args){ int [] array1 = {1,2,3,4,5,6,7,8,9,10}; int [] array2 = new int[10]; //copy array1 into array2 System.arraycopy(array1, 0, array2, 0, 10);

for(int i = 0; i<10; i++) System.out.println(array2[i]);

}}

Output 12345678910

Page 5: Arrays Useful methods

class CopyArray{

public static void main(String [] args){ int [] array1 = {1,2,3,4,5,6,7,8,9,10}; int [] array2 = new int[10]; //copy array1 into array2 System.arraycopy(array1, 0, array2, 0, 10);

//copy last 5 elements in array1 into first 5 of array2 System.arraycopy(array1, 5, array2, 0, 5);

for(int i = 0; i<10; i++) System.out.println(array2[i]);

}}

Output 678910678910

Page 6: Arrays Useful methods

java.util.Arrays class contain lots of useful methods to deal manipulate and searching for searching for data in an array.

i.e. sorting array of integers into ascending order

int myArray[] = {5, 4, 3, 2, 1}; java.util.Arrays.sort(myArray); //myArray now holds 1, 2, 3, 4, 5

Page 7: Arrays Useful methods

import java.util.*;class sort{ public static void main(String [] args){ int [] array1 = {10, 5, 6, 1, 3};

// this will sort array1 in ascending order Arrays.sort(array1);

for(int i = 0; i<5; i++) System.out.println(array1[i]); }}

Output 1356

10

Page 8: Arrays Useful methods

import java.util.*;class sort{ public static void main(String [] args){ String [] array1 = {"ba", "ad", "acd", "efg", "eag"};

// this will sort array1 in ascending order Arrays.sort(array1);

for(int i = 0; i<5; i++) System.out.println(array1[i]); }}

Output

acdadbaeagefg

Page 9: Arrays Useful methods

int myArray[] = new int[5]; java.util.Arrays.fill(myArray, 10); //myArray now holds 10, 10, 10, 10, 10

java.util.Arrays.fill(myArray, 1,3, 20); //myArray now holds 10, 20, 20, 10, 10

Page 10: Arrays Useful methods

import java.util.*;class CopyArray{

public static void main(String [] args){ int [] array1 = new int[5] //fill array1 the value 10 Arrays.fill(array1, 10);

for(int i = 0; i<5; i++) System.out.println(array1[i]);

}}

Output 1010101010

Page 11: Arrays Useful methods

import java.util.*;class CopyArray{

public static void main(String [] args){ int [] array1 = new int[5] //fill array1 the value 10 Arrays.fill(array1, 10);

//fill array1 from the position the indetx 1 to 2(3-1) with the value 10 Arrays.fill(array1, 1, 3, 20);

for(int i = 0; i<5; i++) System.out.println(array1[i]);

}}

Output 1020201010

Page 12: Arrays Useful methods

int myArray[] = {1,2,3,4,5,6,7,8,9,10} java.util.Arrays.binarysearch(myArray, 10); //myArray now holds 10, 10, 10, 10, 10

Page 13: Arrays Useful methods

import java.util.*; class binarysearch{ public static void main(String [] args){ int [] array1 = {10, 5, 6, 1, 3};

// this will sort array1 in ascending order Arrays.sort(array1);

// prints 0 position of the 1st occurrence of 1 System.out.println(Arrays.binarySearch(array1,1));

//prints 1 position of the 1st occurrence of 3 System.out.println(Arrays.binarySearch(array1,3));

// 20 does not exists (prints a negative number) System.out.println(Arrays.binarySearch(array1,20)); }}

Page 14: Arrays Useful methods

Import java.util.*;class binarysearch{ public static void main(String [] args){ int [] array1 = {10, 5, 6, 1, 3};

// this will sort array1 in ascending order Arrays.sort(array1);

// prints 4 position of the 1st occurrence of 10 System.out.println(Arrays.binarySearch(array1,0,5,10));

//searching from position 1 to 3 positions System.out.println(Arrays.binarySearch(array1,1,4,10));

}}

Output 4-4

Page 15: Arrays Useful methods

import java.util.*; class binarysearchString{ public static void main(String [] args){ String [] array1 = {“ba”, “ab”, “aa”, “def”, “daf”};

// this will sort array1 in ascending order Arrays.sort(array1);

// prints 0 position of the 1st occurrence of “aa” System.out.println(Arrays.binarySearch(array1,”aa”));

//prints 1 position of the 1st occurrence of “ab” System.out.println(Arrays.binarySearch(array1,”ab”));

// “Golsmiths” not in array1 (prints a negative number) System.out.println(Arrays.binarySearch(array1, “Goldmisths”)); }}

Page 16: Arrays Useful methods

A number of useful methods to manipulate arrays are defined in this class.

For more information see the following link: http://docs.oracle.com/javase/6/docs/api/java/util/Arrays.html

Page 17: Arrays Useful methods

Advantages Very efficient, quick to access and add to Type-safe, can only add items that match the

declared type of the array Disadvantages

Fixed size, some overhead in copying/resizing Can’t tell how many items in the array, just

how large it was declared to be Limited functionality, need more general

functionality

Page 18: Arrays Useful methods

Write a program that stores the value 20 into all elements of an array is integers.

Page 19: Arrays Useful methods

Write a program that copies the first 5 element of an array of integer of length 10 into the last 5 element of another array of the same length.

Page 20: Arrays Useful methods

Write a code that takes two arrays of integers of the same length and swaps their entire contents.

Page 21: Arrays Useful methods

Write a code that checks if two arrays of integers of the same length are the same.

Page 22: Arrays Useful methods

Write a code that sorts an array of integer in ascending order.

Page 23: Arrays Useful methods

Write a code that sorts an array of strings in ascending order.

Page 24: Arrays Useful methods

Write a code that checks if an integer is contained in array of integers.

Page 25: Arrays Useful methods

Write a code that checks if a string is contained in array of strings.

Page 26: Arrays Useful methods

Given the following declaration String[] cities = {"Washington", "London", "Paris",

"New York"};

Write a program that uses a binary search to search for a particular city is contained in the array cities.

Page 27: Arrays Useful methods

import java.util.*;

public class CityBinarySearch {

public static void main(String [] args) {

String[] cities = new String[]{"Washington", "London", "Paris", "New York"};

//sorting array in java Arrays.sort(cities); //searching on sorted array in java using Arrays binarySearch()

method if(Arrays.binarySearch(cities, "Paris") >=0 ){ System.out.println("Element found on sorted String "+ "array using binary search"); }}}

Page 28: Arrays Useful methods

Write a code that takes an array of integers and prints out the minimum 'gap' between adjacent values in the array. The gap between two adjacent values in a array is defined as the second value minus the first value. If the length of the array is less 2, the program prints out 0.

Page 29: Arrays Useful methods

Example of useful methods defined in ▪ java.util.Arrays class

Practise exercise