J2SE中的一些基础问题总结(不断更新)

来源:互联网 发布:sql必知必会怎么样 编辑:程序博客网 时间:2024/06/13 03:22

int[] arr={1,2,3,4,5};

1.请问怎么转成list?

 

 

 

 

import java.util.ArrayList;

import java.util.List;

 

public class Test {

    public static void main(String[] args) {

        int[] arr={1,2,3,4,5};

       

        List<Integer> li = new ArrayList<Integer>();

        

        for(int i = 0; i < arr.length; i++) {

            li.add(arr[i]);

        }

       

        for(Integer temp : li) {

            System.out.print(temp + " ");

        }

    }

}

 

 

 

 

 

 

2.String s=aa123自中华人民共和国”写一个函数returnStrint  maxByteString s

要求按着输入的字节数取出随机给定的字符串,如果字节为单数(取到的正好是半个汉字)就忽略

returnStr1 s    应该为:a

returnStr2 s            aa

returnStr3 s            aa

returnStr4 s            aa

returnStr5 s            aa1

以此类推。。。。。。

 

 

 

 

 

 

 

public class Test {

 

    /**

     * @param args

     */

    public static void main(String[] args) {

        String s = "aa123自中华人民共和国";

        String result = returnStr(13, s);

        System.out.println(result);

    }

   

    private static String returnStr(int maxByte, String s) {

        byte[] b = s.getBytes();//字符串转化为字节数组

        byte[] temp = new byte[2];//临时字节数组

        String result = "";//结果字符串

        int i = 0;

        boolean flag = true;//标志位,防止漏掉字节

       

        while(i < maxByte){//字节数组循环

           

            flag = true;

           

            if(b[i] >= 0 && b[i] <= 255){//0-255说明是单字节

                result += (char)b[i];//加入结果字符串中

                i++;

            }else if((i + 1 < maxByte) //不是单字节,如果下一位没有超出范围,并且也不是单字节

                    && (b[i+1] < 0 || b[i+1] > 255)){//组成一个汉字

                temp[0] = b[i];//临时字节数组赋值

                temp[1] = b[i+1];

                result += new String(temp);//组成一个汉字,并加入字符串

                flag = false;//如果进来执行过,标志位为假

                i += 2;//跳过一个字节

            }

            

            /*

             * 当汉字的半个字节是最后一位时,此时 i+1==maxByte

             * 上面 i+=2 不会执行造成死循环

             * 所以在这跳出循环

             * 

             *  else if 满足条件 执行 i+=2

             *  可能 i+1 == maxByte 这样就漏掉一个字节

             *  所以用标志位来判断是否进入else if

             *  来判断是否执行这一句

             */

            if(i + 1 == maxByte && flag) {

                break;

            }

       

        }

        return result;

    }

}

 

 

 

 

3.java 中能“重新抛出异常”如:

. . . 

catch(Exception e) {

System.out.println("An exception was thrown");

throw e;

}

既然最后还是重新抛出了,为什么之前要去try...catch它呢?这样重新抛出一下有什么好处呢?

 

 

 

重新抛出意味这我可能需要在这里做一些处理,但是这样的处理并不能解决出现的异常,还需要外部来捕获这个异常.

 

比如你的代码中,当出现异常后,首先要输出一句话表示异常产生(这里就是我要做的处理,当然实际中可能需要更复杂的处理比如数据库回滚之类的),但是同时这个异常不能被我吞掉,我需要继续将他抛出,让外面继续捕获这个异常

 

 

 

 

4.java如何将字符串保存在数组中,然后输出

 

public class numTest {

      public static void main(String args[]) {

        String str = "aaa";

        char [] strArr = str.toCharArray();

        for(int i = 0;i<strArr.length;i++) {

           

            System.out.println("output:" + strArr[i]);

           

        }

       

      }

}

 

 

 

4. 我试了下, 确实是这样. 比如String str = "bcd你好";
char ch = str.charAt(2)返回"d"
char ch = charAt(3)
返回""
这是为什么啊, 难道是UNICODE编码方式存放的, 字母汉字都占二字节.
charAt是返回一个UNICODE字符. charAt返回值明明是char. 这是为什么啊?

 

 

一、Javachar类型采用Unicode编码,一个字符占两个字节,并且这是确定的,不会随硬件架构的变化而变化;
二、charAt(int index)返回的是处在index位置的字符,index0开始;
三、'i'''这两个分别是一个字符,都是占两个字节;

 

5. String a = "";

String b = "";

String c = "";

String d = a + b + c;

System.out.print(d);

/* output

我是人

 



请问如何改变或者通过一定的转换让b的值变成一个空字符? 前提是在转换前b 是一个String 
我想输出

 

 

 

public class testkongge {

 

    String a ="";

    String b ="";

    String c = "";

    String d=a+b+c;

    public static void main(String[] args) {

        testkongge test = new testkongge();

        System.out.print(test.d.replace("",""));

 

    }

 

}

 

 

6.如何在硬盘上创建一个file文件?

 

 

import java.io.File;

import java.io.IOException;

 

 

public class Testcreatfile {

 

    /**

     * @param args

     * @throws IOException

     */

    public static void main(String[] args) throws IOException {

       

        File file = new File("D:/test.txt");

        file.createNewFile();

        System.out.println("创建成功");

    }

 

}

 

7.如何使一个由frame类构成的窗口,按住右上角的“X”按钮,关闭窗口。使用什么的函数,最好是frame类的。大家有什么想法?

import java.awt.*;

public class BorderLayoutEx{

public static void main(String[] args){
Frame f ;
Button b1,b2,b3,b4,b5;
f = new Frame("BorderLayoutEx");
f.setLayout(new BorderLayout());
b1 = new Button("North");
b2 = new Button("South");
b3 = new Button("East");
b4 = new Button("West");
b5 = new Button("Center");
f.add("North",b1);
f.add("South",b2);
f.add("East",b3);
f.add("West",b4);
f.add("Center",b5);
f.setSize(200,200);
f.setVisible(true);
}
}

 

正确办法:

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class BorderLayoutEx{

public static void main(String[] args){
Frame f ;
Button b1,b2,b3,b4,b5;
f
= new Frame("BorderLayoutEx");
f.setLayout(
new BorderLayout());
b1
= new Button("North");
b2
= new Button("South");
b3
= new Button("East");
b4
= new Button("West");
b5
= new Button("Center");
f.add(
"North",b1);
f.add(
"South",b2);
f.add(
"East",b3);
f.add(
"West",b4);
f.add(
"Center",b5);
f.setSize(
200,200);
f.addWindowListener(
new WindowAdapter(){   //使用内部类 用WindowAdapter这个类
    public void windowClosing(WindowEvent e){  //使用windowClosing这个方法
     System.exit(0);
    }
});
f.setVisible(
true);
}
}