Keywords: integrated application of arrays
1. Turning an array into a string
Define the function to convert the elements in the {23,27,998} array to a string "[23,27,998]"
Public Static void toString (int[] arr) {
String temp = "[";
for (int i=0;i<arr.length;i++) {
if (i<arr.length-1) {
Temp = temp + Arr[i] + ",";
}
if (i==arr.length-1) {
Temp = temp + Arr[i] + "]";
}
}
System. out. println (temp);
}
2. Summary: The core idea of using arrays
-Container characteristics: A. Fixed length B. elements are indexed
-When do I use the container? There is a lot of data that must be stored first. And then do the operation.
-When is the array container used? The data is many, the number is definite, and has correspondence relation.
-When analyzing requirements,
1, is there a corresponding ordinal number of the data in the requirement?
2. Does the data in the requirement need an orderly number?
If it does, it is stored in an array.
-Tabular method: There is a correspondence between the data. The other party is queried by a party in the relationship. Use another container map when the corresponding relationship exists, but no one is ordered.
3. Conversion between the binaries
A. Requirements: decimal---16 binary.
Public Static void Main (string[] args) {
int [] arr = {12,23,14,15,16,17};
toString (arr);
System. out. println ("--------------Split Line---------------");
int num = 60;
String str = tohex(num);
System. out. println (str);
System. out. println ("--------------Split Line---------------");
int num2 = 60;
String str2 = tohex_2(num2);
System. out. println (STR2);
}
1. Define the function to convert the elements in the {23,27,998} array to a string "[23,27,998]"
Public Static void toString (int[] arr) {
String temp = "[";
for (int i=0;i<arr.length;i++) {
if (i<arr.length-1) {
Temp = temp + Arr[i] + ",";
}
if (i==arr.length-1) {
Temp = temp + Arr[i] + "]";
}
}
System. out. println (temp);
}
2. Requirements: decimal---16 binary. &15 >>>4 Cycle
eg, 3C
Public Static String tohex (int num) {
2.1. Define the container, which is a character, a length of 8, and an integer of up to 8 16 binary bits.
Char [] CHS = new char[8];
2.2. Define an index to manipulate the array.
int index = chs.length;
2.3. Loop through the array.
while (num!=0) {
int temp = num & 15;
if (Temp > 9) {
Chs[--index] = (char) (temp-10+ ' A ');
}Else{
Chs[--index] = (char) (temp + ' 0 ');
}
num = num >>> 4;
}
return "0X" +toString(CHS);
}
2.4. Define a function to convert the character array into a string
Private Static String toString (char[] arr) {
String temp = "";
for (int i=0;i<arr.length;i++) {
Temp = temp + arr[i];
}
return temp;
}
The improved version of the second question:
/*
Ideas:
Each bit of decimal to 16 binary is one of the hexadecimal elements,
The hexadecimal element is a fixed number, and there is a corresponding number.
So, the legendary tabular method can be used.
*/
Public Static String tohex_2 (int num) {
1. Create a table
Char [] chs = {' 0 ', ' 1 ', ' 2 ', ' 3 '
, ' 4 ', ' 5 ', ' 6 ', ' 7 '
, ' 8 ', ' 9 ', ' A ', ' B '
, ' C ', ' D ', ' E ', ' F '};
2. Create a temporary container
Char [] arr = new char[8];
3. Create a corner label for the action staging container
int index = arr.length;
4. & and >>> operation of num by loop
while (num!=0) {
5. Perform & operations on num
int temp = num & 15;
6. According to the results of the & operation as the angle Index table, to obtain the corresponding characters. and stores the characters in a temporary container.
Arr[--index] = chs[temp];
7. Move num to the right
num = num >>> 4;
}
return "0X" +toString(Arr,index);
}
Private Static String toString (char[] arr, int index) {
String temp = "";
for (int i = index; i < arr.length; i++) {
Temp = temp + arr[i];
}
return temp;
}
B. Summary version: conversion between the binaries
Requirements: decimal---16 binary, end version.
Ideas:
Each bit of decimal to 16 binary is one of the hexadecimal elements.
Hexadecimal elements have a number of fixed numbers. And there is the corresponding number.
So you can use the legendary tabular Method!
4. Summary of the large collection of the system. Conversions of decimal to 2, 8, 16, and so on.
Public Static void Main (string[] args) {
Ten--->2
int num = 76;
String str1 = trans(num, 1, 1);
System. out. println ("Ten--->2:" +str1);
System. out. println ("-------------Split Line----------------");
Ten--->8
String str2 = trans(num, 7, 3);
System. out. println ("Ten--->8:" +str2);
System. out. println ("-------------Split Line----------------");
Ten--->16
String STR3 = trans(num, 15, 4);
System. out. println ("Ten--->16:" +STR3);
System. out. println ("-------------Split Line----------------");
}
The conversion between decimal and binary, octal, hexadecimal. --Ultimate Edition
Public Static String Trans (int num,int base,int offset) {
1. Build the table, the universal table, any system is universal.
Char [] chs = {' 0 ', ' 1 ', ' 2 ', ' 3 '
, ' 4 ', ' 5 ', ' 6 ', ' 7 '
, ' 8 ', ' 9 ', ' A ', ' B '
, ' C ', ' D ', ' E ', ' F '};
2. Set up a temporary container for storing the corresponding number of the binary.
Char [] arr = new char[32];//This is because, if it's binary, it could be 32 bits,
This big thing is OK, anyway, from the back of the deposit, and turn into a string is also from a non-0 position.
3. In turn, to store the number of the angle mark
int index = arr.length;
4. Get the binary number, convert it
while (num!=0) {
int temp = num & base;
Arr[--index] = chs[temp];
num = num >>> offset;
}
5. Convert to String
return toString (Arr,index);
}
Private Static String toString (char[] arr, int index) {
String temp = "";
for (int i = index; i < arr.length; i++) {
Temp = temp + arr[i];
}
return temp;
}
5. Inverse of the array. The insertion point of an ordered array
A. When looking for a number in an array, if the number is not found, it usually returns-1, here-1, which represents a case where the angle mark does not exist.
B. Requirements: If you insert an element into an ordered array and continue to be orderly, ask how to get the position?
Analysis: Since it is an ordered array and is looking for a location, you must think of a binary lookup method.
Answer:
Public Static void Main (string[] args) {
int [] arr = {9,13,22,51,54,78,110};
int index = searchindex(arr, 34);
System. out. println ("The index=" +index);
This is because the length of the array cannot be changed, so you can only get to the insertion point, and you cannot insert this number into the array.
}
If you insert an element into an ordered array and continue to be orderly, ask how do I get the position?
Public Static int searchindex (int[] arr,int key) {
int Max,min,mid;
min = 0;
max = arr.length-1;
while (Min<=max)
{
Mid = (Min+max) >>1;
if (Key>arr[mid])
Min = mid + 1;
Else if (Key<arr[mid])
max = mid-1;
Else
return mid;
}
return min;
}
}
Java Fundamentals Review Day Fourth