Details One: string, switch, default value, array

Source: Internet
Author: User

1. Enter a string s to determine if the string str is equal, using: variable. Equals (variable)

2, A. Equals (b), string type comparison is equal
If A is null, an error will be nullpointerexception
Workaround: Put the determination not to be null in front

3, take Max to Min random number, (Math.random () * (max-min+1) +min)

4. Switch (num) {
Case 1:
System.out.println ("123");
Break
Case 2:
System.out.println ("456");
Break
}
When Num=1, if Case 1 does not break, it will perform a down-penetrating final output: 123
456
5, array of array of type char default value is: Empty (show is a space)
Array entries for integer and floating-point arrays The default value is: 0
Array item default value for array of type string: null
Array item of the array of type Boolean default value is: false

6, String a = "Java";
String B = "Length";
A.compareto (b);
CompareTo compares the order of two strings in the order of their mother, returning the result int type
Compare the first letter with the first letter of B as the x-axis 0 coordinate, the first letter of a J in the first two bits of L, then return-2
If the first letter is the same, the second letter is compared until the last letter is found or compared
Returns 0 if the letters are the same
If string a = "arr";
String B = "Arrs";
A.compareto (b);
The return value is-1

7. Sort the array from small to large
Int[] arr = {5,6,1,9,3,7,21,8,4};
Arrays.sort (arr);
8. Bubble sort
int [] arr = {5,9,3,6,1,7,2,8};
for (int i = 0; i < arr.length-1; i++) {
for (int j = 0; J < Arr.length-1-i; J + +) {
if (Arr[j]>arr[j+1]) {
int a = Arr[j];
ARR[J] = arr[j+1];
ARR[J+1] = A;
}
}
}
9. Exchange Sort
int [] arr = {5,9,3,6,1,7,2,8};
for (int i = 0; i < arr.length-1; i++) {
for (int j = i+1; J < Arr.length; J + +) {
if (Arr[i]>arr[j]) {
int a = Arr[i];
Arr[i] = Arr[j];
ARR[J] = A;
}
}
}
10. Select Sort
int [] arr = {5,9,3,6,1,7,2,8};
for (int i = 0; i < arr.length-1; i++) {
int k = i;
for (int j = k+1; J < Arr.length; J + +) {
if (Arr[j]<arr[k])
K = J;
}
if (i! = k) {
int a = Arr[i];
Arr[i] = arr[k];
Arr[k] = A;
}
}
11, you can consider a two-dimensional array, a point in the Y axis of the x-axis coordinates (INT[Y][X])
Declaring a two-dimensional array
int [] arr = new INT[5][5];
Assign value
Arr[0][0] = 1;
ARR[0][1] = 1;
...
declaring and assigning values
int [] arr = {{1,1,1,1,1},{2,2,2,2,2},{3,3,3,3,3},{4,4,4,4,4},{5,5,5,5,5}};
The first length must be assigned, and the second length can be declared without assigning a value (the length is not fixed)
Statement
int [] arr = new int [3][];
Assignment, you must declare the following length before you can assign a value
Arr[0] = new INT[3];
Traversing a two-dimensional array requires a double loop

Details One: string, switch, default value, 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.