Arrays Useful methods

Preview:

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

Citation preview

Java.util.Arrays class

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

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);

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

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

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

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

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

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

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

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

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

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)); }}

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

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”)); }}

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

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

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

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.

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

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

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

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

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

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

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.

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"); }}}

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.

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

Practise exercise

Recommended