java-basic elements Used (random number, array, sort)

Source: Internet
Author: User

There is no basic function that can be played down in Java.

Random number: Click to open link

1.

Package java.lang.Math.Random; static method Math.random () returns a double of 0.0~1.0

2.

Package java.util.Random; A class

Two ways to construct Random ():

Random (): Creates a new random number generator.

Random (Long Seed): Creates a new random number generator with a single long seed.

The fractional number that generates the [0,1.0] interval: double D1 = r.nextdouble ();
decimal for generating [0,5.0] interval: Double d2 = r.nextdouble () * 5;
Generate decimal for [1,2.5] interval: Double d3 = r.nextdouble () * 1.5 + 1;
Generates an integer from 231 to 231-1: int n = r.nextint ();
Integer that generates the [0,10] interval: int n2 = R.nextint (10);

Array: Click to open link

1.

Each array in Java has a property named length, which represents the size of the array.

The length property is public final int, that is, length is read-only. Once the array length is determined, the size cannot be changed.

2.

You cannot use the Equals function directly, because the default is still the comparison is the same object.

3.

When an array element is not a primitive native data type, it holds the reference type, not the object itself. When the object is generated, the reference points to the object, otherwise the reference is NULL

Type []a=type [2]; A[0]=new Type (); A[1]=new Type ();

4.

Two-dimensional variable-length arrays

<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:12PX;" >public class arraytest4{public    static void Main (string[] args)    {        //two-D variable-length array        int[][] A = new int[3][];        a[0] = new int[2];        A[1] = new Int[3];        A[2] = new int[1];        Error: Cannot empty the first dimension        //int[][] b = new int[][3];}    } </span>


Sort by:

If you use the C + + STL Direct sort fix

For basic array Arrays.sort (a); Reverse order Arrays.sort (Strarray, Collections.reverseorder ());

Ignoring case: Arrays.sort (Strarray, String.case_insensitive_order);

If you want to sort an array of objects, implement the Java.util.Comparator interface yourself.

Comparator to override compare method in Comparator interface import Java.io.*;import java.net.*;import java.util.*;p ublic class test{public static void Main (String args[]) throws exception{  treemap<string,integer> mm=new treemap<string,integer> ( New Comparator () {public   int compare (Object a,object b) {    string aa= (String) A;    String bb= (string) b;    return Bb.compareto (AA);   }  });  Mm.put (New String ("a"), new Integer);  Mm.put ("B", new Integer);  Set<map.entry<string,integer>> Set=mm.entryset ();  SYSTEM.OUT.PRINTLN (set);  System.out.println (Mm.keyset ()); }}

Enclose the code for the quick and stacked rows:

Import java.util.arrays;import java.util.collections;import java.math.*;p ublic class Test {static int n=100;static int [ ] a=new int[n];static int [] b=new int[n];static int [] c=new int[n];p ublic static void qsort (int []a,int Low, Int. high) {I F (low>=high) return;int l =low,h=high;int t=a[l];while (l


java-basic elements Used (random number, array, sort)

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.