The basic knowledge of array in Java Learning tutorial _java

Source: Internet
Author: User
Tags natural logarithm square root wrapper

Digital

Typically, when we work with numbers, we use the original data type, such as byte,int,long,double.

Example

int i = 5000;
float GPA = 13.65;
byte mask = 0xaf;

However, in development, we encounter situations where objects need to be used rather than the original data type. To implement this, Java provides a wrapper class for each raw data type.

All wrapper classes (Integer, Long, Byte, Double, Float, short) are subclasses of the abstract class number.

This wrapper is handled by the compiler, which is called boxing. Therefore, when an original data type is used, and when an object is needed, the compiler puts the original data into its wrapper class. Similarly, the compiler can also fetch objects out of the original data type. Number is part of the Java.lang package.

Here is an example of boxing and unboxing:

public class test{public

  static void Main (String args[]) {
   integer x = 5;//boxes int to an integer object
   x = x + ten;  Unboxes the Integer to a int
   System.out.println (x); 
  }


This will produce the following results:

Copy Code code as follows:
15

When x is assigned an integer value, the compiler puts the integer in the box because X is an integer object. Then, X is removed so that they can be added as integers.

Number method
The following is a list of instance methods implemented in all subclasses implemented by the number class:

SN Method Description
1 Xxxvalue ()
The value of this number object is converted to the data type of XXX and returns
2 CompareTo ()
Compare this number object with the parameter
3 Equals ()
Determines whether this numeric object is equal to the parameter
4 ValueOf ()
Returns an integer object holding the specified original value
5 ToString ()
Returns a String object that represents the value of the specified int or integer
6 parseint ()
This method is used to get the original data type of a string
7 ABS ()
Returns the absolute value of a parameter
8 Ceil ()
The smallest integer returned is greater than or equal to the parameter. Back to Double
9 Floor ()
The largest integer returned is less than or equal to this parameter. Back to Double
10 Rint ()
The returned integer, which is the nearest value to the parameter value. Back to Double
11 Round ()
Returns the nearest long or int by the argument of the return type of the method
12 Min ()
Returns the smaller of the two parameters.
13 Max ()
Returns the larger of the two parameters.
14 EXP ()
Returns the base e of the natural logarithm, the power of the parameter
15 Log ()
Returns the natural logarithm of a parameter
16 POW ()
Returns the power value of the first argument to the second argument
17 sqrt ()
Returns the square root of a parameter
18 Sin ()
Returns the sine value of the specified double value
19 cos ()
Returns the cosine of the specified double value
20 Tan ()
Returns the tangent of the specified double value
21st ASIN ()
Returns the string that is the inverse of the specified double value
22 ACOs ()
Returns the inverse cosine of the specified double value
23 Atan ()
Returns the tangent value of the specified double value
24 ATAN2 ()
Converts a rectangular coordinate (x,y) to polar coordinates (r,θ) and returns theta
25 Todegrees ()
Converts a parameter to a degree
26 Toradians ()
Converts a parameter to radians
27 Random ()
Returns a random number


Array
Java provides a data structure: an array that stores a fixed sized contiguous set of elements of the same type. Arrays are collections that store data, but tend to think of arrays as a collection of variables of the same type.

In contrast to declaring a single variable, such as NUMBER0, Number1, ... number99, declare an array variable, such as numbers and use Numbers[0, Numbers[1] ..., numbers[99] to represent each variable.

This tutorial will explain how to declare an array variable by using an index variable, create an array, and work with an array.

Declaring an array variable
to use an array of programs, you must declare a variable to refer to the array, and you must specify the type that the variable of the array can reference. Here is the syntax for declaring an array variable:

Datatype[] Arrayrefvar;  Preferred way.

Or

DataType arrayrefvar[]; Works but not preferred way.

Note Style datatype[] Arrayrefvar is preferred. Style DataType arrayrefvar[] comes from the C + + language, which facilitates Java to inherit the C + + programming style.

Example

The following code fragment is an example of this syntax:

Double[] myList;     Preferred way.

Or

Double mylist[];     Works but not preferred way.

Create an array
You can create an array using the following syntax by using the new operator:

Arrayrefvar = new Datatype[arraysize];

The above statement does two things:

    • It creates an array that uses the new datatype[arraysize];
    • It assigns the newly created array reference to the variable Arrayrefvar.

Declaring an array variable, creating an array, and assigning it to a variable array reference can be combined in one statement, as follows:

datatype[] Arrayrefvar = new Datatype[arraysize];

Alternatively, you can create an array, as follows:

Datatype[] Arrayrefvar = {value0, value1, ..., valuek};

Array elements are accessed through the index. The subscripts of an array start at 0, that is, they start at 0 to Arrayrefvar.length-1.

Example

The following statement declares an array variable myList, creates an array of 10 elements of the double type, and assigns its reference to MyList:

double[] myList = new DOUBLE[10];

The following picture represents the array myList. Here, the myList has 10 double values, and the index is from 0 to 9.

Working with arrays
when working with array elements, loop loops or foreach loops are often used, because all elements in an array are of the same type and the size of the array is known.

Example

Here's a complete example that shows how to create, initialize, and process arrays:

public class Testarray {public

  static void Main (string[] args) {
   double[] myList = {1.9, 2.9, 3.4, 3.5};

   Print all the array elements for
   (int i = 0; i < mylist.length i++) {
     System.out.println (Mylist[i] + ""); c6/>}
   //Summing all elements
   double total = 0;
   for (int i = 0; i < mylist.length i++) {Total
     + = Mylist[i];
   }
   SYSTEM.OUT.PRINTLN ("Total are" + total);
   Finding the largest element
   double max = mylist[0];
   for (int i = 1; i < mylist.length i++) {
     if (Mylist[i] > max) max = mylist[i];
   System.out.println ("Max is" + max);
  }


This will produce the following results:

1.9
2.9
3.4
3.5 Total are
11.7
Max is 3.5

foreach Loop
JDK 1.5 introduces a new for loop called a Foreach loop or an enhanced for loop that does not use an index variable to complete the traversal of an array.

Example

The following code shows all the elements in the array myList:

public class Testarray {public

  static void Main (string[] args) {
   double[] myList = {1.9, 2.9, 3.4, 3.5};

   Print all the array elements for
   (double element:mylist) {
     System.out.println (element);
   }
  }
}

This will produce the following results:

1.9
2.9
3.4
3.5

Passing an array to a method
You can also pass an array to a method, just as you pass a basic type value. For example, the following method displays an element in an int array:

public static void PrintArray (int[] array) {for
 (int i = 0; i < Array.Length; i++) {
  System.out.print (array[ I] + "");
 }

You can call it by passing an array. For example, the following statement calls method PrintArray to display 3,1,2,6,4,2:

PrintArray (New int[]{3, 1, 2, 6, 4, 2});

Returns an array from a method
a method can also return an array. For example, the method shown below returns an array, which is an inversion of another array:

public static int[] Reverse (int[] list) {
 int[] result = new Int[list.length];

 for (int i = 0, j = result.length-1 i < list.length; i++, j--) {
  result[j] = List[i];
 }
 return result;
}

Arrays class
the class in Java.util.Arrays contains a variety of static methods for sorting and searching arrays, comparison of arrays, and populating array elements. These methods are overloaded for all base types.

SN Method and Description
1 public static int BinarySearch (object[] A, Object key)
Use the binary search algorithm to search the specified array of objects (bytes, integers, doubles, etc.) to specify values. The array must be categorized before this call is made. If it is included in the list (-(insertion point + 1), the index search keyword is returned.
2 public static Boolean Equals (Long[] A, long[] A2)
Returns true if the two specified arrays of bulls are equal to each other. Two arrays are considered equal to the determination method if two arrays contain the same number of elements and are equal to all corresponding pairs of two array elements. Returns true if two arrays are equal. The same method can be used for all other raw data types (Byte, short, Int, etc.)
3 public static void Fill (int[] A, int val)
Converts the specified int value to each element in the specified int array. The same method can be used for all other raw data types (Byte, short, Int etc.)
4 public static void sort (object[] a)
Arranges an array in ascending order, depending on the nature of its elements. The same method can be used for all other raw data types (Byte, short, Int, etc.)

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.