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];
}


}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值