2013年8月14日、數組第二種定義格式|遍歷|最值|

sorry  今天遲到了、

今天收到第一份評價很感激、我會堅持、努力的、謝謝您、

/*
給定一個數組,對其進行反轉。


{3,1,6,5,8,2} --> 
{2,8,5,6,1,3};


其實就是頭尾元素的位置置換。


*/




class  ArrayTest
{
public static void printArray(int[] arr)
{
System.out.print("[");
for(int x=0; x<arr.length; x++)
{
if(x!=arr.length-1)
System.out.print(arr[x]+", ");
else
System.out.println(arr[x]+"]");
}
}
public static void main(String[] args) 
{
int[] arr = {4,1,8,7,3,8,2};
printArray(arr);
reverseArray(arr);
printArray(arr);
}


public static void reverseArray(int[] arr)
{
for(int start=0,end=arr.length-1; start<end; start++,end--)
{
swap(arr,start,end);
}
}
public static void swap(int[] arr,int a,int b)
{
int temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
}

————————————————————————————————





/*
獲取一個整數的16進制表現形式。
*/


class ArrayTest2 
{
public static void main(String[] args) 
{
toHex_2(0);
}


// 0,1,2,3,4,5,6,7,8,9,A,  B, C, D, E, F
// 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15


/*
什麼時候使用數組呢?
如果數據出現了對應關係,而且對應關係的一方是有序的數字編號。並作爲角標使用。
這時就必須要想到數組的使用。

就可以將這些數據存儲到數組中。 
根據運算的結果作爲角標直接去查數組中對應的元素即可。


這種方式:稱爲查表法。







*/




public static void toHex_2(int num)
{


if(num==0)
{
System.out.println("0");
return ;
}
//定義一個對應關係表。
char[] chs = {'0','1','2','3',
'4','5','6','7',
'8','9','A','B',
'C','D','E','F'};
/*
一會查表會查到比較的數據。
數據一多,就先存儲起來,在進行操作。
所以定義一個數組。 臨時容器。
*/
char[] arr = new char[8];
int pos = arr.length;


while(num!=0)
{
int temp = num&15;
arr[--pos] = chs[temp];
num  = num >>> 4;
}


System.out.println("pos="+pos);
for(int x=pos ;x<arr.length; x++)
{
System.out.print(arr[x]);
}




}






public static void toHex_1(int num)
{
//定義一個對應關係表。
char[] chs = {'0','1','2','3',
'4','5','6','7',
'8','9','A','B',
'C','D','E','F'};

for(int x=0 ; x<8; x++)
{
int temp = num & 15;
System.out.print(chs[temp]);
num = num >>> 4;
}


}


public static void toHex(int num)
{


for(int x=0; x<8; x++)
{
int temp = num & 15;
if(temp>9)
System.out.print((char)(temp-10+'A'));
else
System.out.print(temp);
num = num >>> 4;
}
/*
int n1 = num & 15;
System.out.println("n1="+n1);


num = num >>> 4;
int n2 = num & 15;
System.out.println("n2="+n2);
*/
}
}

————————————————————————————————————

class ArrayTest3 
{
public static void main(String[] args) 
{
// toHex(26);
toBinary(-6);
// toOctal(26);
System.out.println(Integer.toBinaryString(-6));
}


//十進制-->十六進制。
public static void toHex(int num)
{
trans(num,15,4);
}
//十進制-->二進制。
public static void toBinary(int num)
{
trans(num,1,1);
}
//十進制-->八進制。
public static void toOctal(int num)
{
trans(num,7,3);
}


public static void trans(int num,int base,int offset)
{


if(num==0)
{
System.out.println("0");
return ;
}
//定義一個對應關係表。
char[] chs = {'0','1','2','3',
'4','5','6','7',
'8','9','A','B',
'C','D','E','F'};
/*
一會查表會查到比較的數據。
數據一多,就先存儲起來,在進行操作。
所以定義一個數組。 臨時容器。
*/
char[] arr = new char[32];
int pos = arr.length;


while(num!=0)
{
int temp = num & base;
arr[--pos] = chs[temp];
num  = num >>> offset;
}


for(int x=pos ;x<arr.length; x++)
{
System.out.print(arr[x]);
}
System.out.println();


}


}

class ArrayTest4 
{
public static void main(String[] args) 
{
String week = getWeek(71);
System.out.println(week);
}
/*
使用查表法。
星期。
String s = "abc";
int x = 4;
*/
public static String getWeek(int num)
{


if(num>7 || num<1)
{
return "錯誤的星期";
}
String[] weeks = {"","星期一","星期二","星期三","星期四","星期五","星期六","星期日"};


return weeks[num];
}


}


發佈了33 篇原創文章 · 獲贊 3 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章