Java Learning Note 04--array

Source: Internet
Author: User

Java Learning Note 04--array

The method of array copying is to use the arraycopy () method provided by the system class with the following syntax:

System.arraycopy (object src, int srcpos, object dest, int destpos, int length);

System.arraycopy ( source Array, starting position in source array, target array, starting position in target data, number of array elements to copy);

[Java]View Plaincopy
  1. Public class T {
  2. public static void Main (String args[]) {
  3. int arr1[] = {1,2,3,4,5};
  4. int arr2[] = new int[5];
  5. System.arraycopy (arr1, 0, ARR2, 0, 5);
  6. For (int i = 0; i < arr2.length; i++) {
  7. System.out.println (Arr2[i]);
  8. }
  9. }
  10. }


Arrays class

Some basic operations of an array, such as sorting, searching, and comparing, are common. The array is provided in Java to assist you with these operations, array is a class located in the Java.util package, and he provides several methods that can be used directly.

Sort () helps you sort the specified array by using the Quick Sort method

BinarySearch () allows you to search a sorted array for $ two, and returns the index of the value if the specified value is found, otherwise a negative number is returned

Fill () When you configure an array, the default value is given according to the data type. For example, an array of integers has an initial value of 0, and you can use the Arrays.fill () method to set all elements to the specified value

Equals () compares the values of the elements in the two array if they are all equal, and returns true if they are, otherwise false

[Java]View Plaincopy
  1. Import Java.util.Scanner;
  2. Import Java.util.Arrays;
  3. Public class T {
  4. public static void Main (string[] args) {
  5. Scanner Scanner = new Scanner (system.in);
  6. int[] arr = { 5, 3, 2, 7, 91};
  7. System.out.print ("before sorting:");
  8. For (int i = 0; i < arr.length; i++) {
  9. System.out.print (Arr[i] + "");
  10. }
  11. System.out.println ();
  12. Arrays.sort (arr);
  13. System.out.print ("after sorting:");
  14. For (int i = 0; i < arr.length; i++) {
  15. System.out.print (Arr[i] + "");
  16. }
  17. System.out.print ("\ n Please enter search value:");
  18. int key = Scanner.nextint ();
  19. int find =-1;
  20. if (find = Arrays.binarysearch (arr, key)) >-1) {
  21. System.out.println ("Find value in Index" + Find + "location");
  22. } Else
  23. System.out.println ("No specified value found");
  24. }
  25. }

Execution Result:

[Java]View Plaincopy
    1. before sorting:  93 5  3 55 57 7  2 73 41  91&NBSP;&NBSP;&NBSP;
    2. after sorting:  2 3 5  7 41 55 57  73 91 93   
    3. Enter search value: 5  
    4. found value in index  2  location   


The following uses arrays to populate and compare arrays

[Java]View Plaincopy
  1. Import Java.util.Arrays;
  2. Public class T {
  3. public static void Main (string[] args) {
  4. int[] arr1 = new int[10];
  5. int[] arr2 = new int[10];
  6. int[] Arr3 = new int[10];
  7. Arrays.fill (ARR1, 5);
  8. Arrays.fill (ARR2, 5);
  9. Arrays.fill (ARR3, 10);
  10. System.out.print ("arr1:");
  11. For (int i = 0; i < arr1.length; i++) {
  12. System.out.print (Arr1[i] + "");
  13. }
  14. System.out.println ("\narr1 = arr2?" + arrays.equals (arr1, arr2));
  15. System.out.println ("arr1 = arr3?" + arrays.equals (arr1, ARR3));
  16. }
  17. }

Execution results

[Java]View Plaincopy
      1. ARR1: 5 5 5 5 5 5 5 5 5 5
      2. arr1 = arr2? true
      3. arr1 = ARR3? false

Java Learning Note 04--array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.