Home > Java > javaTutorial > Regaining the basics of Java (14): Summary of arrays

Regaining the basics of Java (14): Summary of arrays

黄舟
Release: 2017-01-16 10:15:19
Original
1430 people have browsed it

Regain the basics of java (14): Summary of arrays

1. Sorting of arrays

 数组排序的核心是:对数组的遍历。     
 1、冒泡排序         
 A、冒泡排序一共需要排序arr.length-1次;具体需要多少轮不确定   这个也需要            
 循环  按最多的轮数进行循B、冒泡排序其实就是arr.lengh-1次数组从第一个开始进行连接两个数的依次比较。           
 相邻的两个数比大小,小的放前面,大的放后面(交换位置)         
 代码为:
 int[] arr = { 6, 9, 8, 3, 4, 6, 8, 1, 2 };        
 for (int i = 0; i < arr.length; i++) {            
 for (int j = 0; j < arr.length - 1; j++) {                
 if (arr[j] < arr[j+1]) {                    
 int temp = arr[j];                    
 arr[j] = arr[j + 1];                    
 arr[j + 1] = temp;                
 }            
 }        
 }        
 for (int i : arr) {            
 System.out.print(i + " ");        
 }       
 2、选择排序          
 A、每一轮反复要做:求出最小值,然后把最小值放到前面(交换位置)       
 第1轮    把最小的    发到   arr[0]       
 第2轮    把次小的    发到   arr[1]          
 B、具体需要多少轮不确定    这个也需要循环  按最多的轮数进行              
 循环  数组.length-1          
  其代码:    
  int[] arr = { 8, 6, 5, 3, 4, 2, 9, 6, 5, 7, 4 };        
  for (int i = 0; i < arr.length - 1; i++) {            
  for (int j = i + 1; j < arr.length; j++) {                
  if (arr[i] > arr[j]) {                    
  int temp = arr[i];                    
  arr[i] = arr[j];                    
  arr[j] = temp;                
  }            
  }        
  }        
  for (int i : arr) {            
  System.out.print(i + " ");        
  }
Copy after login

2. Arrays Find

1、顺序查找算法             
标签的重要性!        
int key=5;        
boolean flag=true;        
for (int i = 0; i < arr.length; i++) {            
if(key==arr[i]){                
System.out.println("yes");                
flag=false;                
break;            
}        
}        
if (flag) {           
 System.out.println("oh,no");        
 }        
 2、二分折半查找算法A、 先确定一个范围,然后找中间只,接下来分三种情况比较B、循环次数不确定  建议用while   
 while(left<=right){        
 循环操作        
 //1.找中间值        
 //2.分三种情况进行比较   
 }
 C、 该算法要求数组必须是有序的    
 // 折半排序        
 int key = 9;        
 int min = 0;        
 int max = arr.length-1;        
 int mid = (min + max) / 
 2;        
 boolean flag = true;////      
 while (key != arr[mid]) {//          
 if (key > arr[mid]) {//              
 min = mid + 1;//          
 }//          
 if (key < arr[mid]) {//              
 max = mid - 1;//          
 }//          
 if (max < min) {//              
 System.out.println("这个数不存在!");//              
 flag = false;//              
 break;//          
 }//          
 mid = (min + max) / 
 2;//      
 }//      
 if (flag) {//          
 System.out.println("这个数在" + mid + "位置。");//      
 }        
 while (min <= max) {            
 if (key < arr[mid]) {                
 max = mid - 1;            
 }            
 if (key > arr[mid]) {                
 min = mid + 1;            
 } else {                
 System.out.println("这个数在" + mid + "位置。");                
 flag = false;                
 break;            
 }            
 mid = (min + max) / 2;        
 
 }        
 if (flag) {            
 System.out.println("这个数在" + (mid+1) + "位置。");        
 }
Copy after login

3. Arrays class

  1. This class is a tool class that mainly contains some functions for operating data Methods, such as sorting, searching, etc.

2. java.util package

3. Static function method public static void sort(xxx[] a), for arrays a is sorted in ascending order public static int binarySearch(xxx[] a , xxx key), and the key in the array a is searched through the binary search method public static xxx[] copyOf(xxx[] a , int newLength) , by copying the array a The newLength length of data is copied to the new array public static xxx[] copyOfRange(xxx[] a , int from , int to), by copying the specified range of data of array a to the new array public static boolean equals(xxx[] a1 , xxx [] a2), compare whether the two arrays a1 and a2 are the same

4. Date class

  1. The date class in Java, specially used To store data related to date and time

2. java.util package

3. 1 second = 1000 milliseconds

4. Use the time indicator Yes, this method still exists, but it is not recommended to use. To ensure forward compatibility

5. The Date class was not designed with internationalization in mind

6. Construction method public Date(), a parameterless constructor that can create a Date object containing the current date and time (Western format: Thu Oct 30 15:38:46 CST 2014) public Date(long time), a parameterized constructor that can create a Date object that is time milliseconds since 1970-1-1

7. Function method public long getTime() returns the number of milliseconds represented by this Date object since January 1, 1970 00:00:00 GMT . Commonly used for comparison between two dates.

5. Calendar class

  1. This class is the calendar class in Java

2. Some obsolete methods in the Date class have been replaced by methods of this class

3. java.util package

4. This class is an abstract class and cannot create objects through new Calendar c =Calendar.getInstance();

5. public static Calendar getInstance(), Get and return an instance object of this class

6. Function method public int get(int field) returns The value of the given field in the calendar object public void set(int field, int value), sets the value of the given field in the calendar object public final Date getTime(), returns the date and time object in the calendar

6. SimpleDateFormat class: Date formatting class

1. format:格式化 (控制格式或风格)
2. 该类专门用于控制日期时间数据的格式
3. 不同国家或地区使用的日期时间的格式是不一样的
4. 创建出来的对象叫格式化器
5. java.text包
6. 构造方法public SimpleDateFormat(),用默认语言环境的日期格式public SimpleDateFormat(String pattern),用给定的日期格式
7. 功能方法public final String format(Date date),将一个 Date对象格式化为日期/时间字符串。
public Date parse(String source),将字符串解析(转换)为Date对象a.必须进行异常处理
b.该方法要求格式化器对象的格式跟字符串格式必须一样        
// 让用户在控制台输入“dd/mm/yyyy”格式的日期,        
// // 然后人工拼接成“yyyy-mm-dd”格式,最后按“xxxx年x月xx日"              
格式输出 
String s = "1992/08/24"; 
SimpleDateFormat s1 = new SimpleDateFormat("yyyy/MM/dd"); 
Date date = s1.parse(s); 
SimpleDateFormatsDate = new SimpleDateFormat("yyyy-MM-dd"); 
String s2 = sDate.format(date); 
SimpleDateFormat pDate = new SimpleDateFormat("yyyy年MM月dd日"); 
String s3 = pDate.format(date); 
System.out.println("人工拼接成:" + s2 + ",最后输出:" + s3);    
// // 3.计算从今天起,100天后是几月几号,        
// 并以xxxx年xx月xx日的格式输出来        
// (提示:查阅帮助文档自学Calendar类的add方法)        
Calendar c =Calendar.getInstance();        
c.add(Calendar.DAY_OF_YEAR, 100);        
Date date = c.getTime();    
SimpleDateFormat s = new SimpleDateFormat("yyyy年MM月dd日");        
String s1=s.format(date);        
System.out.println(s1);
Copy after login


## The above is the content. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template