请在一个类中编写一个方法,这个方法搜索一个字符数组中是否存在某个字符,如果存在,则返回这个字符在字符数组中第一次出现的位置(序号从0开始计算),否则,返回-1。要搜索的字符数组和字符都以参数形式传递传递给该方法,如果传入的数组为null,应抛出IllegalArgumentException异常。在类的main方法中以各种可能出现的情况测试验证该方法编写得是否正确,例如,字符不存在,字符存在,传入的数组为null等。 
2
请用移位的方式打印出一个十进制整数的十六进制形式。提示:按每4个二进制位对整数进行移位和去高位处理,得到的结果就是十六进制数的一位,然后按下面三种方式之一(作为作业,要求每种方式都用到)计算出一个十六进制数值对应的十六进制形式: 
1)0-9之间的数值直接加上字符'0',9以上的数值减去10以后再加上字符'A' 
2)定义一个数组,其中包含0-F这些字符,然后用要计算的数值作为数组的索引号,即可获得其对应的十六进制数据。 
3)Character.forDigit静态方法可以将一个十六进制的数字转变成其对应的字符表示形式,例如,根据数值15返回字符'F'。 

解决方案 »

  1.   

    http://community.csdn.net/Expert/topic/5470/5470193.xml?temp=.1414453典型的作业帖
    不过你比那人大方多了
      

  2.   

    public static int indexOf(char[] args,char c){
    if(args==null||args.length<1)
    throw new IllegalArgumentException();
    int index=-1;
    for(int i=0;i<args.length;i++){
    if(c==args[i]){
    index=i;
    break;
    }
    }
    return index;
    }
      

  3.   

    这是 Integer.toHexString(int i)源代码 自己研究去
        final static char[] digits = {
    '0' , '1' , '2' , '3' , '4' , '5' ,
    '6' , '7' , '8' , '9' , 'a' , 'b' ,
    'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
    'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
    'o' , 'p' , 'q' , 'r' , 's' , 't' ,
    'u' , 'v' , 'w' , 'x' , 'y' , 'z'
        };
        public static String toHexString(int i) {
    return toUnsignedString(i, 4);
        }
        private static String toUnsignedString(int i, int shift) {
    char[] buf = new char[32];
    int charPos = 32;
    int radix = 1 << shift;
    int mask = radix - 1;
    do {
        buf[--charPos] = digits[i & mask];
        i >>>= shift;
    } while (i != 0); return new String(buf, charPos, (32 - charPos));
        }
      

  4.   

    1.
        public static int findChar(char[] chars, char ch) {
            if (chars == null) {
                throw new IllegalArgumentException();
            }
            return Arrays.binarySearch(chars, ch);
        }
    2.
    懒得看
      

  5.   

    static public void setArr(char[] _arr,char x){
    int a = -1;
    if(_arr==null){
    throw new java.lang.IllegalArgumentException();
    }
        
    for (int i = 0; i<_arr.length; i++){
    if(_arr[i]==x){
    if(a==-1){
    a=i;
    }
    }
    }
    System.out.println (a);
    }
      

  6.   

    tai nan la, don't they?????
      

  7.   

    public class DecToHex{public static char table[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};    public static void main(String[] args)
        {
        int sourceDec=Integer.parseInt(args[0]);
        int length=0;
        int[] digits=new int[20];
        while(sourceDec>=16)
        {
        digits[length++]=sourceDec&0x0000000f;
        sourceDec>>=4;
        }
        digits[length]=sourceDec;
        for(int i=length;i>=0;i--)
        {
        System.out.print(table[digits[i]]);
        }
        }}