1,n=2,m=3,請將n和m的值互換。
①,int t; t=n; n=m; m=t;(最常用的方式)
②,n=n+m; m=n-m;n=n-m;(當n和m的值很大時,n+m的值可能超出int的范圍,從而使結(jié)果失去精度)
③,n=nm;m=nm;(相當于m=(nm)m);
n=nm;(相當于n=n(n^m));(其利用了一個數(shù)同時異或另一個數(shù)兩次,結(jié)果仍是它自身的特性)
2,選擇排序(升序)
public static void selectSort(int[ ] arr)
{
for(int x=0;x<arr.length-1;x++)
{
for(int y=x+1;y<arr.length;y++)
{
if(arr[x]>arr[y])
{
int temp=arr[x];
arr[x]=arr[y];
arr[y]=temp;
}
}
}
}
3,冒泡排序(降序)
public static void bubbleSort(int[ ] arr)
{
for(int x=0;x<arr.length-1;x++)
{
for(int y=0;y<arr.length-x-1;x++)
{
if(arr[y]<arr[y+1])
{
int temp=arr[y];
arr[y]=arr[y+1];
arr[y+1]=temp;
}
}
}
}
4,插入排序
public static int[] charu(int[] a){
for(int i=1;i<a.length;i++){
for(int j=i-1;j>=0;j--){
int min=a[j+1];
if(min<a[j]){
a[j+1]=a[j];
a[j]=min;
}
}
}
return a;
}
5,歸并排序
public class t3 {
public static void main(String[] args) {
int arr[] = {4,3,2,9,7,8,6,5,4};
split(arr,0,arr.length-1);
}
public static void split(int[] arr,int left,int right){
int mid = (right+left)/2;
if(left<right){
split(arr,left,mid);
split(arr,mid+1,right);
merger(arr,left,mid,right);
}
}
public static void merger(int[] arr,int left,int mid, int right){
int low1=left;
int low2=mid+1;
int temp[] = new int[right-left+1];
int index=0;
while(low1<=mid&&low2<=right){
if(arr[low1]<arr[low2]){
temp[index++]=arr[low1++];
}else{
temp[index++]=arr[low2++];
}
}
while(low1<=mid){
temp[index++]=arr[low1++];
}
while(low2<=right){
temp[index++]=arr[low2++];
}
for (int i = 0; i < index; i++) {
arr[left++]=temp[i];
}
System.out.println(Arrays.toString(arr));
《java測試題合集》
1、使用除余法,計算 128 和 -257 的內(nèi)存二進制值。寫出計算過程。
1、128 的二進制計算過程
2、-257 的二進制計算過程
2、使用三種以上的循環(huán)方式實現(xiàn)99乘法表。寫出思路與實現(xiàn)代碼。
1、for 循環(huán)實現(xiàn)99乘法表
2、while 循環(huán)實現(xiàn)99乘法表
3、do whole 循環(huán)實現(xiàn)99乘法表
3、寫一個方法,使用 Scanner 傳入一個字母,如果是大寫,輸出小寫,反之輸出大寫。寫出思路與實現(xiàn)代碼。
1、思路
2、實現(xiàn)代碼
4、寫一個方法,傳入一個 Date 日期類型的參數(shù),輸出今天是星期幾。寫出思路與實現(xiàn)代碼。
1、思路
2、實現(xiàn)代碼
5、數(shù)組排序-冒泡排序。寫出思路與實現(xiàn)代碼。
1、思路
2、實現(xiàn)代碼
6、數(shù)組排序-選擇排序。寫出思路與實現(xiàn)代碼。
1、思路
2、實現(xiàn)代碼
7、數(shù)組排序-插入排序。寫出思路與實現(xiàn)代碼。
1、思路
2、實現(xiàn)代碼
8、數(shù)組排序-歸并排序。寫出思路與實現(xiàn)代碼。
1、思路
2、實現(xiàn)代碼
9、寫一個方法,計算任意兩個數(shù)組的交集。寫出思路與實現(xiàn)代碼。
1、思路
2、實現(xiàn)代碼
10、寫一個方法,計算任意兩個數(shù)組的并集。寫出思路與實現(xiàn)代碼。
1、思路
2、實現(xiàn)代碼
11、寫一個計算器類,要求能夠計算兩個任意類型(整型、浮點型)參數(shù)的和。寫出思路與實現(xiàn)代碼。
1、思路
2、實現(xiàn)代碼
12、定義一個點類(Point)和一個線類(Line),計算線的長度。寫出思路與實現(xiàn)代碼。
1、思路
2、實現(xiàn)代碼
13、定義一個接口,提供開和關(guān)兩個功能。定義一個手機類,使用該接口。寫出思路與實現(xiàn)代碼。
1、思路
2、實現(xiàn)代碼
14、寫一個方法,打印出指定目錄里面的目錄和文件的結(jié)構(gòu)。寫出思路與實現(xiàn)代碼。
// 可能使用到的 File 實例對象方法 isDirectory,getName,getPath。
1、思路
2、實現(xiàn)代碼
15、定義一個 Login 類,Scanner 輸入用戶名和密碼。如果用戶不存在,拋出用戶還未注冊的異常信息(使用自定義異常)。如果用戶存在且密碼正確,提示登錄成功,否則提示登錄失敗。寫出思路與實現(xiàn)代碼。
1、思路
2、實現(xiàn)代碼