`
825197453
  • 浏览: 101095 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

java编程题

阅读更多
package com.zhoubo;   
/**  
 * 用迭代的方法,判断是不是一个回文字符串,如”abdba”  
 * @author BZ70000910  
 *  
 */  
public class AbcbA {   
    public static void main(String [] args){   
        AbcbA instance = new AbcbA();   
        System.out.println(instance.isAbcbA("abcefg"));   
    }   
  
    public boolean isAbcbA(String str){   
        while(str.length() > 1){   
            int length = str.length();   
            char first = str.charAt(0);   
            char last = str.charAt(length - 1);   
            if(first == last){   
                String strcpy = str.substring(1, length - 1);   
                System.out.println(strcpy);                
                isAbcbA(strcpy);   
            }else{   
                return false;                  
            }   
            //不加break,就会进入死循环,因为退出循环也是一层一层的。做改错题肯定有意思。   
            break;   
        }   
        return true;   
    }   
}  


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/njnu_zhoubo/archive/2009/10/13/4663866.aspx

 

package com.zhoubo;   
/**  
 *如何用java读取大文件  
 */  
import java.io.RandomAccessFile;   
import java.nio.ByteBuffer;   
import java.nio.channels.FileChannel;   
  
public class ReadBigFile {   
  
    /**  
     * @param args  
     */  
    public static void main(String[] args)throws Exception {   
        //定义缓冲区的大小为1KB   
        int bufSize = 1024;   
        byte [] bs = new byte[bufSize];   
           
        ByteBuffer byteBuffer = ByteBuffer.allocate(bufSize);   
        FileChannel channel = new RandomAccessFile("c:\\dmmsi.log","r").getChannel();   
        int size;   
        //读取到输入流的最后   
        while((size = channel.read(byteBuffer))!=-1){   
            byteBuffer.rewind();   
            byteBuffer.get(bs);   
            System.out.println(new String(bs, 0, size));   
            byteBuffer.clear();            
        }   
        channel.close();   
           
    }   
  
}  


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/njnu_zhoubo/archive/2009/10/13/4663866.aspx
package com.zhoubo;   
  
import java.io.BufferedReader;   
import java.io.IOException;   
import java.io.InputStreamReader;   
import java.nio.CharBuffer;   
  
/**  
 * 读取字符串序列,如果有“abcd“,则返回true.  
 * 提供一个每次读一个字符的函数  
 * public char getChar()  
 * @author BZ70000910  
 *  
 */  
public class SimenseTest {   
    static CharBuffer temp = CharBuffer.allocate(1024);   
  
    /**  
     * @param args  
     */  
    public static void main(String[] args)throws IOException {   
        String str = "ABCD";   
        InputStreamReader isr = new InputStreamReader(System.in);   
        BufferedReader br = new BufferedReader(isr);           
        br.read(temp);   
        SimenseTest st = new SimenseTest();   
        System.out.println(st.findString(str));   
  
    }   
       
    public char getChar()throws IOException{           
        temp.flip();   
        char x = temp.get();   
        temp.compact();   
        return x;   
    }   
  
    public boolean findString(String str)throws IOException{   
        char temp;   
        int i = 0;   
        while((temp = getChar())!=0){   
//          System.out.println(temp);   
            if(temp == str.charAt(i)){   
                if(i == 3){   
                    return true;   
                }else  
                    i++;   
            }else  
                i = 0;   
               
        }   
        return false;   
    }   
}  


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/njnu_zhoubo/archive/2009/10/13/4663866.aspx

 

package com.zhoubo;   
/**  
 *编写一个class,提供void addElement(int element)方法,  
 *提供int maxi(),提供int mini(),提供 int elementLength(),提供中间值int middle()  
 */  
import java.util.ArrayList;   
import java.util.Arrays;   
  
public class SimenseTest2 {   
    private ArrayList<Integer> arrayList;   
       
    public SimenseTest2(ArrayList<Integer> arrayList) {   
        this.arrayList = arrayList;   
    }   
  
    public void addElement(int element){   
        arrayList.add(element);   
    }   
       
    public int maxi(){   
        int max = arrayList.get(0) ;   
        for(int i = 1; i < arrayList.size(); i++){   
            if(max < arrayList.get(i))   
                max = arrayList.get(i);   
        }   
        return max;   
    }   
       
    public int mini(){   
        int mini = arrayList.get(0);   
        for(int i = 1; i < arrayList.size(); i++){   
            if(mini > arrayList.get(i))   
                mini = arrayList.get(i);   
        }   
        return mini;   
    }   
       
    public int length(){   
        return arrayList.size();   
    }   
       
    public int middle(){   
        int middle = 0;   
        int size = arrayList.size();   
        Integer [] array = (Integer [])arrayList.toArray(new Integer[size]);   
        Arrays.sort(array);   
        return array[size/2];   
    }   
    /**  
     * @param args  
     */  
    public static void main(String[] args) {   
        ArrayList<Integer> arrayList = new ArrayList<Integer>();   
        SimenseTest2 st2 = new SimenseTest2(arrayList);   
        st2.addElement(2);st2.addElement(3);st2.addElement(4);   
        st2.addElement(5);st2.addElement(6);   
        System.out.println(st2.maxi());   
        System.out.println(st2.mini());   
        System.out.println(st2.middle());   
    }   
  
}  


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/njnu_zhoubo/archive/2009/10/13/4663866.aspx

 

package com.zhoubo;   
/**  
 *用stringtokenizer的方法,建立一个replace()方法  
 */  
import java.util.StringTokenizer;   
  
public class StringReplace {   
  
    /**  
     * @param args  
     */  
    public static void main(String[] args) {   
        StringReplace sr = new StringReplace();   
        System.out.println("orilly ooh heolloo".replace('o', 'e'));   
        System.out.println(sr.CharReplace('o', 'e', "orilly ooh heolloo"));   
  
    }   
       
    public String CharReplace(char oldChar, char newChar, String str){   
        StringTokenizer stk = new StringTokenizer(str, Character.toString(oldChar));   
        System.out.println(stk.countTokens());   
        StringBuffer temp = new StringBuffer();   
        //判断替换字符是否在开头   
        if(str.charAt(0) == oldChar)   
            temp.append(newChar);   
        while(stk.hasMoreElements()){   
            temp.append(stk.nextToken());   
            temp.append(newChar);   
        }   
        //判断替换字符是否在结尾   
        if(!(str.charAt(str.length()-1) == oldChar))   
            temp.deleteCharAt(temp.length()-1);   
        //如果多个重复的被替换   
        return temp.toString();   
    }   
  
}  


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/njnu_zhoubo/archive/2009/10/13/4663866.aspx

 

package com.zhoubo;   
/**  
 *提供一个字符串,转化成10进制输出,例如“AA”,输出170  
 * @author BZ70000910  
 *  
 */  
public class ZTETest {   
  
    /**  
     * @param args  
     */  
    public static void main(String[] args) {   
        String str = "ABCDE" ;   
        ZTETest test = new ZTETest();   
        System.out.println(test.decimal(str));   
    }   
  
    public int decimal(String str){   
        char [] chars;   
        if(str != null){   
            chars = str.toCharArray();   
        }else  
            chars = null;   
        int sum = 0;   
        int j = 0;   
        for(int i = str.length()-1; i>=0 ; i--){   
            sum = sum + (getValue(chars[i])<<(4*j));   
            j++;   
        }   
        return sum;   
    }   
       
    public int getValue(char x){   
        switch(x){   
            case '0': return 0;   
            case '1': return 1;   
            case '2': return 2;   
            case '3': return 3;   
            case '4': return 4;   
            case '5': return 5;   
            case '6': return 6;   
            case '7': return 7;   
            case '8': return 8;   
            case '9': return 9;   
            case 'A': return 10;   
            case 'B': return 11;   
            case 'C': return 12;   
            case 'D': return 13;   
            case 'E': return 14;   
            case 'F': return 15;   
            default: return 0 ;   
        }   
    }   
}  


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/njnu_zhoubo/archive/2009/10/13/4663866.aspx

 

package com.zhoubo;   
/**  
 * 编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。  
 *  但是要保证汉字不被截半个,如“我ABC”4,应该截为“我AB”,  
 *  输入“我ABC汉DEF”,6,应该输出为“我ABC”而不是“我ABC+汉的半个”。  
 * @author BZ70000910  
 *  
 */  
public class ZTETest2 {   
  
    /**  
     * @param args  
     */  
    public static void main(String[] args) {   
        String str = "我abc汉def";   
        ZTETest2 test = new ZTETest2();   
        System.out.println(str.substring(0,4));   
        System.out.println(test.StringCutByByte(str, 2));   
  
    }   
       
    public String StringCutByByte(String str, int index){   
        String temp = null;   
        byte [] bytes = str.getBytes();   
        byte [] bytetemp = new byte[index];   
        for(int i=0; i<index ; i++ ){   
            bytetemp[i] = bytes[i];   
            System.out.println(bytes[i]);   
        }   
        if(index>1){   
            if(bytes[index-1]<0&&bytes[index-2]>0){              
                //第三个参数表示要解码的字节数   
                temp = new String(bytetemp,0,index-1);             
            }else{   
                temp = new String(bytetemp);   
            }          
        }else{   
            if(bytes[index-1]<0){   
                temp = null;   
            }else  
                temp = new String(bytetemp,0,1);   
        }   
           
        return temp;   
    }   
  
}  
package com.zhoubo;
/**
 * 编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。
 *  但是要保证汉字不被截半个,如“我ABC”4,应该截为“我AB”,
 *  输入“我ABC汉DEF”,6,应该输出为“我ABC”而不是“我ABC+汉的半个”。
 * @author BZ70000910
 *
 */
public class ZTETest2 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String str = "我abc汉def";
		ZTETest2 test = new ZTETest2();
		System.out.println(str.substring(0,4));
		System.out.println(test.StringCutByByte(str, 2));

	}
	
	public String StringCutByByte(String str, int index){
		String temp = null;
		byte [] bytes = str.getBytes();
		byte [] bytetemp = new byte[index];
		for(int i=0; i<index ; i++ ){
			bytetemp[i] = bytes[i];
			System.out.println(bytes[i]);
		}
		if(index>1){
			if(bytes[index-1]<0&&bytes[index-2]>0){			
				//第三个参数表示要解码的字节数
				temp = new String(bytetemp,0,index-1);			
			}else{
				temp = new String(bytetemp);
			}		
		}else{
			if(bytes[index-1]<0){
				temp = null;
			}else
				temp = new String(bytetemp,0,1);
		}
		
		return temp;
	}

}
 


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/njnu_zhoubo/archive/2009/10/13/4663866.aspx

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics