当前位置:网站首页>12.26 exercise summary
12.26 exercise summary
2022-06-12 04:59:00 【なんでもないゃ】
1、 Traversing a one-dimensional array --for each
package test;
public class Test01 {
public static void main(String[] args) {
int ef[]={1,2,3,4,5,6};
for(int a:ef){
System.out.print(a);//123456
}
}
}
Be careful :for(int a:ef) Things represented in .
2、Arrays class -- Copy (arraycopy), Sort (Arrays.sort), lookup (Arrays.binarysearch), fill (Arrays.fill)
package test;
import java.util.Arrays;
public class Test02 {
public static void main(String[] args) {
int a[]={0,0,0,0,0,0};
int b[]={1,2,3};
System.arraycopy(a,1,b,1,2);
for(int c:b){
System.out.print(c);//100
}
System.out.println();
int A[]={1,3,2,4,6,5};
Arrays.sort(A);
for(int m:A){
System.out.println(m);//123456
}
System.out.println(Arrays.toString(A));//[1, 2, 3, 4, 5, 6]
System.out.println(Arrays.binarySearch(A,2));//1
System.out.println(Arrays.binarySearch(A,0));//-1 No return found -1
Arrays.fill(A,2,3,100);
System.out.println(Arrays.toString(A));//[1, 2, 100, 4, 5, 6]
}
}Be careful :1 Copy is to a Copy the contents to b in , Don't go against the target . namely arraycopy Method is the copied object , The second array is the copy object .2 binarySearch() It uses dichotomy to find the position of the element , If the index of the returned element is found , If you don't find it, go back -1.3 Arrays.fill() Execute before exporting , Direct output will report an exception . explain Array.fill(A,2,3,100): Yes A Fill in the elements in , Subscript to be 2-3, But not including 3, The filling element is 100.
2、Object An array stores table data
import java.util.Arrays;
public class Test03 {
public static void main(String[] args) {
Object a1[]={1," full name "," Gender "," Age "};
Object a2[]={2," Zhang San "," male ",10};
Object a3[]={3," Li Si "," Woman ",18};
System.out.println(Arrays.toString(a1));
System.out.println(Arrays.toString(a2));
System.out.println(Arrays.toString(a3));
}
}Object Arrays can hold different types of content
3、javabean And array to save table data
package test;
import java.util.Arrays;
public class Test04 {
public static void main(String[] args) {
Bean[] bean = {
new Bean(" Xiaohong ", 18, " Casting engineer ", 10000),
new Bean(" Xiao Ming ", 21, " Software engineer ", 10000),
new Bean(" floret ", 23, " Hardware Engineer ", 10000)
};
System.out.println(Arrays.toString(bean));//[ Xiaohong 18 Casting engineer 10000, Xiao Ming 21 Software engineer 10000, floret 23 Hardware Engineer 10000]
for(Bean b:bean){
System.out.println(b);
}
// Xiaohong 18 Casting engineer 10000
// Xiao Ming 21 Software engineer 10000
// floret 23 Hardware Engineer 10000
}
}
class Bean{
private String name;
private int age;
private String job;
private int salary;
public Bean(String name,int age,String job,int salary){
this.name=name;
this.age=age;
this.job=job;
this.salary=salary;
}
public String toString(){
return name+"\t"+age+"\t"+job+"\t"+salary;
}
}
Be careful :class Bean Cannot create on class Test04 in , If you want to 1 Output what you want , Need to be right toString Method ,Javabean What does that mean? , Where does it reflect ?
3、 Sorting of objects --Comparable Interface
package test;
import java.util.Arrays;
public class Test05 {
public static void main(String[] args) {
Com[] com={
new Com(1," Zhang San "," Woman "," building engineer "),
new Com(3," Wang Wu "," male "," Casting engineer "),
new Com(2," Li Si "," male "," Software engineer ")
};
for(Com c:com){
System.out.println(c);
}
//1 Zhang San Woman building engineer
//3 Wang Wu male Casting engineer
//2 Li Si male Software engineer
Arrays.sort(com);
for(Com c:com){
System.out.println(c);
}
//1 Zhang San Woman building engineer
//2 Li Si male Software engineer
//3 Wang Wu male Casting engineer
}
}
class Com implements Comparable{
private int number;
private String name;
private String gender;
private String job;
public String toString(){
return number+"\t"+name+"\t"+gender+"\t"+job;
}
public Com(int number,String name,String gender,String job){
super();
this.number=number;
this.name=name;
this.gender=gender;
this.job=job;
}
public int compareTo(Object o){
Com com=(Com)o;
if(this.number<com.number){
return -1;
}
if(this.number>com.number){
return 1;
}
return 0;
}
}stay javabean On the basis of , There are more things about sorting , To implement sorting, you need to call Arrays.sort(); Class needs to inherit Comparable Interface , Therefore, we need to add super(); And to compareTo Method .
4、 Bubble sorting and optimization
package test;
import java.util.Arrays;
public class Test06 {
public static void main(String[] args) {
int[] a={1,4,2,5,8,9,7};
int temp;
for(int i=0;i<a.length;i++){
for(int j=0;j<a.length-1-i;j++){
if (a[j]>a[j+1]){
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
System.out.println(Arrays.toString(a));
}
}Be careful :j<a.length-1-i; If you don't -1, There will be ArrayIndexOutOfBoundsException abnormal .
package test;
import java.util.Arrays;
public class Test06 {
public static void main(String[] args) {
int[] a={1,4,2,5,8,9,7};
int temp=0;
for(int i=0;i<a.length;i++){
boolean flag=true;
for(int j=0;j<a.length-1-i;j++){
if (a[j]>a[j+1]){
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
flag=false;
}
}
if(flag){
break;
}
}
System.out.println(Arrays.toString(a));
}
}The above code optimizes bubble sorting , Avoid repeated comparisons , it is to be noted that boolean flag=true Wait in for The position of the loop , If the position is incorrect , You can't achieve your goal !
5、 Dichotomy to find elements in an array
package test;
import java.util.Arrays;
public class Test07 {
public static void main(String[] args) {
int arr[]={1,4,6,8,7,9,2,3};
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
System.out.println(binarySearch(arr,4));
}
public static int binarySearch(int array[],int value){
int low=0;
int high= array.length-1;
while(low<=high){
int middle=(low+high)/2;
if(value<middle){
high=middle-1;
}
else if(value>middle){
low=middle+1;
}else{
return middle;
}
}
return -1;
}
}Be careful :return -1 The location of ,int middle=(low+high)/2 Need to change every time .
边栏推荐
- Some problems of silly girl solved
- [backtracking based on bit operation] queen n problem 2
- Google reinforcement learning framework seed RL environment deployment
- File contains (regardless of suffix) Apache log remote file contains PHP encapsulated pseudo protocol:
- The master programmer "plays" a C program that is not like C
- 2022-02-28 WPF upper computer 126 understand mqtt
- Interview must ask: summary of ten classic sorting algorithms
- Qinglong wool - Kaka
- Link: fatal error lnk1168: cannot open debug/test Solution of exe for writing
- Report on the current market situation and future development trend of adhesive industry for radar and ultrasonic sensors in the world and China 2022 ~ 2028
猜你喜欢

Operation of simulated examination platform for theoretical question bank of G2 utility boiler stoker in 2022

JWT学习与使用

Ubunt 20.04 uses CDROM or ISO as the installation source

In the era of smart retail, Weimeng reshapes the value of "shopping guide"

Jwt Learning and use

Simple Tetris

Harris corner detection principle-

2022“高考记忆” 已打包完成,请查收!

When the build When gradle does not load the dependencies, and you need to add a download path in libraries, the path in gradle is not a direct downloadable path

L1-067 Roche limit (10 points)
随机推荐
BI 如何让SaaS产品具有 “安全感”和“敏锐感”(上)
Three. JS import model demo analysis (with notes)
one billion one hundred and eleven million one hundred and eleven thousand one hundred and eleven
Day17 array features array boundary array application traversal array multidimensional array creation and traversal arrays operation array bubble sort
The third "World War" - chip defense, smokeless battlefield!
Applet pull-down load refresh onreachbottom
How to deploy dolphin scheduler 1.3.1 on cdh5
In the era of smart retail, Weimeng reshapes the value of "shopping guide"
MySQL5.7.21 Build For ARM
Radiometric calibration and atmospheric correction of sentry 2 L1C multispectral data using sen2cor
Ten trends of Internet Security in 2022 industry released
Betteland introduces milk products of non animal origin, which will be launched in the U.S. market in the near future
Day18 creation and restoration of sparse array
Parallelization of accelerated training tf data. Dataset generator
LabVIEW关于TDMS和Binary存储速度
2022 fusion welding and thermal cutting recurrent training question bank and simulation examination
Redis learning notes (continuously updating)
File contains (regardless of suffix) Apache log remote file contains PHP encapsulated pseudo protocol:
Realease package appears – missing type parameter
[backtracking] backtracking to solve subset problems