1 / 29

Arrays Useful methods

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.

knox-abbott
Download Presentation

Arrays Useful methods

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java.util.Arrays class Arrays Useful methods

  2. Use of ready methods to manipulate arrays. • java.lang.System class • java.util.Arrays class

  3. Java Arrays – Copyingarraycopy() • 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);

  4. Example arraycopy(Object src, int srcPos, Object dest, int destPos, int length) • 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 1 2 3 4 5 6 7 8 9 10

  5. Example 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 6 7 8 9 10 6 7 8 9 10

  6. Java Arrays – Sorting • 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

  7. ExampleSorting an array of integers 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 1 3 5 6 10

  8. ExampleSorting an array of strings 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 acd ad ba eag efg

  9. Java Arrays – fill 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

  10. Example 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 10 10 10 10 10

  11. Example 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 10 20 20 10 10

  12. Java Arrays – binary search 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

  13. Example binarySearch(Object[] a, Object key) 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)); } }

  14. ExamplebinarySearch(Object[] a, int fromIndex, int toIndex, Object key) 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

  15. Example Binary Search for array of strings 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”)); } }

  16. Java.util.Arrays class • 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

  17. Java Arrays • 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

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

  19. Exercise • 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.

  20. Practise exercisesSwapAll.java • Write a code that takes two arrays of integers of the same length and swaps their entire contents.

  21. Practise exercisesSameArray.java • Write a code that checks if two arrays of integers of the same length are the same.

  22. Practise exercisesAscendingInt.java • Write a code that sorts an array of integer in ascending order.

  23. Practise exercisesAscendingString.java • Write a code that sorts an array of strings in ascending order.

  24. Practise exercises IntegerIn.java • Write a code that checks if an integer is contained in array of integers.

  25. Practise exercises StringIn.java • Write a code that checks if a string is contained in array of strings.

  26. Search an 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.

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

  28. Practise exercisesMinGap.java • 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.

  29. Summary • Example of useful methods defined in • java.util.Arrays class • Practise exercise

More Related