java面试题
1. /**
* 编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。
* 但是要保证汉字不被截半个,如“我ABC”4,应该截为“我AB”,输入“我ABC汉DEF”,6,
* 应该输出为“我ABC”而不是“我ABC+汉的半个
*/
public static String spitStringByByte(String str, int num){
String returnStr = null;
if("".equals(str)){
System.out.println("请重新输入...");
}else{
if( num < 0){
System.out.println("请输入大于0的数字!");
}else{
byte[] strBytes = str.getBytes();
if(strBytes[num] < 0){
while(strBytes[num] < 0){
num--;
}
returnStr = new String(strBytes,0,++num);
}else{
returnStr = new String(strBytes,0,num);
}
}
}
return returnStr;
}
2./**
* 内部类 的调用
*/
public class Outer {
private int n = 10;
public class Inner{
private int m = 10;
}
public static void main(String[] args){
Outer outer = new Outer();
Outer.Inner inner = outer.new Inner();
System.out.print(inner.m);
}
}
3./**
* 金额转换,阿拉伯数字的金额转换成中国传统的形式如:(¥1011)->(一千零一拾一元整)输出
*/
private static final char[] data = new char[] { '零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖' };
private static final char[] units = new char[] { '元', '拾', '佰', '仟', '万', '拾', '佰', '仟', '亿' };
public static String convert(int money){
StringBuffer sb = new StringBuffer();
int until = 0;
while(money != 0){
sb.insert(0, units[until++]);
int number = money%10;
sb.insert(0, data[number]);
money /= 10;
}
return sb.toString();
}
4./**
* 有数组a[n],用java代码将数组元素顺序颠倒
*/
public static String[] listTest(String[] str) {
String temp = "";
int length = 0;
if (str.length % 2 == 0) {
length = str.length / 2;
} else {
length = (str.length / 2) + 1;
}
for (int i = 0; i < length; i++) {
temp = str[str.length - 1 - i];
str[str.length - 1 - i] = str[i];
str[i] = temp;
}
return str;
}
5 /**
* 第1个人10,第2个比第1个人大2岁,依次地退,第8个人多大?
*/
public static int years(int fristYear, int n) {
if (n == 1) {
return fristYear;
}
return fristYear + (n - 1) * 2;
}
6 /*
* 冒泡排序
*/
public static int[] maoPao(int[] j){
int temp = 0;
for(int i=0; i<j.length; i++){
for (int k = i; k < j.length; k++) {
if(j[i] > j[k]){
temp = j[i];
j[i] = j[k];
j[k] = temp;
}
}
}
return j;
}
7. /**
* try catch fianlly
* @param args
*/
public static int tryTest(){
int m = 6;
int n = 12;
try {
System.out.println("try:"+(n-m));
return (n-m);
//System.exit(0);
} catch (Exception e) {
// TODO: handle exception
System.out.println("---catch----");
} finally{
System.out.println("fianlly:"+(m-n));
return (m-n);
}
}
8./**
* 进制之间的转化
* 10 --> 16 Integer.toHexString(this)
* 10 --> 8 Integer.toOctalString(this)
* 10 --> 2 Integer.toBinaryString(this)
*
* 2 --> 10 Integer.parseInt(this, 2)
* 8 --> 10 Integer.parseInt(this, 8)
* 16--> 10 Integer.parseInt(this, 16)
*/
/**
9 * 两个数字相加
*/
public static double add(double v1, double v2) {
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.add(b2).doubleValue();
}
10. /**
* 根据.properties文件中key读取value的值
* @param key
* @return
*/
public static String getPropertyValue(
String key)throws Exception{
String value = null;
try {
String filename = "test";
ResourceBundle rb = ResourceBundle.getBundle(filename);
value = rb.getString(key).trim();
} catch (MissingResourceException e1) {
System.out.println("没有对应的属性!");
//e1.printStackTrace();
} catch (Exception e){
e.printStackTrace();
}
return value;
}
/**
11. * 修改或添加.properties文件属性
* @param key
* @param value
* @throws Exception
*/
public static void writerPreperties(String key, String value)throws Exception{
Properties prop = new Properties(); //属性集合对象
FileInputStream in = new FileInputStream("resource/test.properties"); //属性文件流
prop.load(in); //将属性文件流装载到Properties对象中
in.close();
prop.setProperty(key, value);
FileOutputStream out = new FileOutputStream("resource/test.properties"); //属性文件流
prop.store(out, "randy");
out.close();
}

浙公网安备 33010602011771号