java programming problem

2010-06-07  来源:本站原创  分类:Java  人气:173 

package com.zhoubo;
/**
 *  Use the iterative approach, it is not a Palindromic string  , If  "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;
            }
            // Do not break, it will loop  , Because the exit the loop is also a layer with a layer of correcting the problem certainly do interesting  .
            break;
        }
        return true;
    }
}  

 This article from CSDN blog  , Reprinted, with the citation  :http://blog.csdn.net/njnu_zhoubo/archive/2009/10/13/4663866.aspx
package com.zhoubo;
/**
 * How to read large files using 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 {
        // Define the size of the buffer to  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;
        // Read to the end of the input stream
        while((size = channel.read(byteBuffer))!=-1){
            byteBuffer.rewind();
            byteBuffer.get(bs);
            System.out.println(new String(bs, 0, size));
            byteBuffer.clear();
        }
        channel.close();   

    }   

}  

 This article from CSDN blog  , Reprinted, with the citation  :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;   

/**
 *  Reads a string sequence, if any  "abcd", Returns  true.
 *  Provides a read a character function
 * 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;
    }
}  

 This article from CSDN blog  , Reprinted, with the citation  :http://blog.csdn.net/njnu_zhoubo/archive/2009/10/13/4663866.aspx
package com.zhoubo;
/**
 * Write a class, provide  void addElement(int element) Method  ,
 * Provide  int maxi(), Provide  int mini(), Provide   int elementLength(), Provide intermediate values  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());
    }   

}  

 This article from CSDN blog  , Reprinted, with the citation  :http://blog.csdn.net/njnu_zhoubo/archive/2009/10/13/4663866.aspx
package com.zhoubo;
/**
 * Use stringtokenizer method  , The establishment of a  replace() Method
 */
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();
        // Determine whether the alternative character at the beginning
        if(str.charAt(0) == oldChar)
            temp.append(newChar);
        while(stk.hasMoreElements()){
            temp.append(stk.nextToken());
            temp.append(newChar);
        }
        // Determine whether the alternative character at the end
        if(!(str.charAt(str.length()-1) == oldChar))
            temp.deleteCharAt(temp.length()-1);
        // If multiple duplicate replaced
        return temp.toString();
    }   

}  

 This article from CSDN blog  , Reprinted, with the citation  :http://blog.csdn.net/njnu_zhoubo/archive/2009/10/13/4663866.aspx
package com.zhoubo;
/**
 * Provides a string, converted to  10 Binary output, for example  "AA", Output  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 ;
        }
    }
}  

 This article from CSDN blog  , Reprinted, with the citation  :http://blog.csdn.net/njnu_zhoubo/archive/2009/10/13/4663866.aspx
package com.zhoubo;
/**
 *  Write a string of the function, the interception is entered as a string and the number of bytes  , The output is a string of interception by bytes  .
 *   But to ensure that Chinese characters should not be cut by half the, such as  " My ABC "4, should be truncated to  " I  AB",
 *   Enter "I  ABC Han-DEF ", 6, should be output as  " My ABC "instead  " My ABC + half of Han  ".
 * @author BZ70000910
 *
 */
public class ZTETest2 {   

    /**
     * @param args
     */
    public static void main(String[] args) {
        String str = " My abc-Han  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){
                // The third parameter indicates the number of bytes to decode
                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;
/**
 *  Write a string of the function, the interception is entered as a string and the number of bytes  , The output is a string of interception by bytes  .
 *   But to ensure that Chinese characters should not be cut by half the, such as  " My ABC "4, should be truncated to  " I  AB",
 *   Enter "I  ABC Han-DEF ", 6, should be output as  " My ABC "instead  " My ABC + half of Han  ".
 * @author BZ70000910
 *
 */
public class ZTETest2 {

        /**
         * @param args
         */
        public static void main(String[] args) {
                String str = " My abc-Han  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){
                                // The third parameter indicates the number of bytes to decode
                                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;
        }

}

 This article from CSDN blog  , Reprinted, with the citation  :http://blog.csdn.net/njnu_zhoubo/archive/2009/10/13/4663866.aspx
相关文章
  • java programming problem 2010-06-07

    package com.zhoubo; /** * Use the iterative approach, it is not a Palindromic string , If "abdba" * @author BZ70000910 * */ public class AbcbA { public static void main(String [] args){ AbcbA instance = new AbcbA(); System.out.println(instance.i

  • The basic structure of Java programming 2010-03-29

    This series of documents with the previous "JBuilder development practitioners of the Road" series launch, standing on the level of language itself for reference by them to learn Java programming language. Main reference Java online documentatio

  • JAVA Programming specifications 2008-05-28

    JAVA Programming specifications 1. Applications This specification applies to projects using J2EE specification, all items in the JAVA code (including JSP, SERVLET, JAVABEAN, EJB) shall comply with this specification. At the same time, it can be used

  • How to build a robust code, java programming rules 30 (2) 2010-04-07

    How to build a robust code, java programming rules 30 (2) Source: Network reproduced :2009-6-15 (16) If the initialization process need to override (cancel) finalize (), please remember calling super.finalize () (Object belongs to us if the direct su

  • How to build a robust code, java programming rules 30 (1) 2010-04-07

    How to build a robust code, java programming rules 30 (1) Source: Network reproduced :2009-6-15 This is some pretty good advice! Each rule is very weighty! Is a summary of long-term accumulation of experience, hoping to help you make your high-qualit

  • Collection of common errors in JAVA programming 2010-05-14

    This carried over: http://hi.baidu.com/zzcc_8/blog/item/23f83a43dda0611f72f05d02.html The project encountered an error (c9bcf64c) sql error Find out Unknown column 'fillMen' in 'field list' 2. Are included in the sql statement was made 2 Column 'fill

  • Little experience in JAVA programming 2010-06-05

    Little experience in JAVA programming The following will provide some of the JAVA program design and coding, in order to improve the performance of JAVA programs, which often use some of the methods and techniques. One. Object generation and the size

  • (Transfer) JAVA programming and how to optimize code to improve performance JAVA 2010-07-20

    By using a number of complementary tools to find process bottlenecks, and bottlenecks on the part of the code can be optimized. There are two options: to optimize the code or change the design. We usually choose the latter, because the do not call th

  • [Change] JAVA communication problem between threads 2010-08-22

    [Change] JAVA communication problem between threads Problem In the previous section, describes the use of multi-threaded programming the importance of synchronization mechanisms, and learn how to synchronize the method to correctly access shared reso

  • Reading Notes Java programming ideas 2010-09-15

    Chapt 6 All non-primitive object has a toString () method, when the compiler needs a String object and it is time, the compiler automatically calls the method. Can be rewritten to its (overwrite) For each class to write main function for testing, as

  • Thoughts on the Java programming advice 2010-09-20

    Java programs written notes, advice on Java programming ideas. (1) the class name the first word should be capitalized. Fields, methods, and object (handle) the first letter should be lowercase. For all identifiers, which contains all of the words sh

  • Java programming language commonly used in the method of updating XML documents 2010-09-28

    Quoted from http://www.ibm.com/developerworks/cn/java/l-javaxml/ This paper briefly discusses the Java programming language update XML documents in the four commonly used methods, and analysis of the merits of the four methods. Secondly, the article

  • Generic Java Programming Guide 2010-11-11

    Generic Java Programming Guide This series of articles translated from SUN's generic programming guide, can not read the original translation see http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf I. Introduction JDK1.5 JAVA language on several e

  • Fourth edition of Java programming idea to download the full Chinese 2011-01-09

    "Java programming ideas Fourth Edition" (Thinking in Java (4th Edition)) Download Chinese version of the full Resources on the eDonkey, download back looked like them, and bookmarks have been good, and very easy to share with you now! This is ve

  • JAVA communication problem between threads 2011-01-11

    JAVA communication problem between threads There are many practical problems requires not only want to synchronize access shared resources, but also each contain between threads, communicate with each other to move forward through. So, how many threa

  • java programming 'for performance' of some areas as far as possible 2011-08-25

    Recently the machine memory and full, and out of the new machine's memory, but also a good review should look at our code, there are a lot of coding too freely of these bad habits or lack of understanding of the programming language should be careful

  • Java path problem 2009-05-25

    Path has met with some questions, It is truly worry about , it is determined to solve this problem. Search online at the following number of data for future use 1: What is the path to clarify URI contains RRL + URN URI: Universal Resource Identifier

  • [Change] Java programming in the UDP protocol 2010-04-30

    1. UDP protocol definition The full name of the UDP protocol is user datagram in the network with the TCP protocol as it is used to process packets. In the OSI model, in the fourth floor - the transport layer, the upper layer in the IP protocol. Ther

  • java programming examples of multithreading 2010-04-04

    java programming examples of multi-threaded Java training 2009-07-23 16:10:51 learn to read 307 comments 0 font size: large, medium here that we do a complete example to illustrate the thread returned by different methods and the resulting difference

  • JAVA Programming 3 Performance Optimization 2010-05-13

    JAVA Programming 3 3 Performance Optimization Performance Optimization JAVA Programming