校招筆試之2012金山快盤春季校招java筆試題

1,簡述javaStringStringBuffer,StringBuild的區別。 java

   String:不可變類,線程安全;拼接字符串的時候容易產生大量碎片。 數組

   StringBuffer:字符串緩衝類,可變類,不會產生字符串碎片問題。線程安全。
安全

   StingBuild:字符串緩衝類,可變類,也不會產生字符串碎片問題。線程不安全。
app

    在不考慮線程安全的狀況下,StringBuild的效率最高,StringBuffer次之,String最差。 dom

2,java中要讓一個方法不被子類覆蓋,應該怎麼定義。 ide

    定義方法爲final類型:
函數

    定義爲靜態方法:
ui

class T{
	public static void print(){
		System.out.println("T");
	}
}
class T2 extends T{
	public static void print(){
		System.out.println("T2");
	}
}



3,  找出第二大的數

假設有一個整數數組,請實現一個函數,找出其中第二大的數。(注:請給出你的解題思路和完整實現) this

public static int getSecondNum1(int[] array){
		int r=-1;
		int max = array[0];
		if(array.length<2){
			throw new RuntimeException("數組長度不夠!");
		}else {
			r = array[0];
			for(int i=0;i<array.length;i++){
				if(array[i]>max){
					r = max;
					max = array[i];
				}
			}
		}
		return r;
	}

4, K階斐波那契數列第N項的值:略過了。。 spa

5,日誌類

咱們在軟件開發活動,特別是服務端程序中,爲了分析程序的一些運行狀況,常常須要在代碼內插入一些日誌記錄語句。如今請你設計一個日誌類KPLogs,方便開發使用。

該條日誌等級Level:

package com.perfecking.log;

public enum Level {
	info,debug,warning,error
}

日誌類接口Log:

package com.perfecking.log;

public interface Log {
	public void log(Level level,String info);
	public void info(String info);
	public void debug(String info);
	public void warinig(String info);
	public void error(String info);
}

KPLogs類的實現:

package com.perfecking.log;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Random;

public class KPLogs implements Log{
	private String logName = null;
	private String logPath = null;
	{
		String cPath = System.getProperty("user.dir");
		logPath = cPath+"\\log\\";
		checkFileDir(logPath);
	}
	public KPLogs(){
		
	}
	public KPLogs(String logName){
		if(!logName.endsWith(".txt")){
			logName+=".txt";
		}
		this.logName = logName;
	}
	
	public KPLogs(String logName,String logPath){
		if(!logName.endsWith(".txt")){
			logName+=".txt";
		}
		this.logName = logName;
		if(!logPath.endsWith("\\")){
			logPath+="\\";
		}
		this.logPath = logPath;
	}
	public void setLogName(String logName){
		if(!logName.endsWith(".txt")){
			logName+=".txt";
		}
		this.logName = logName;
	}
	public void setLogPath(String logPath){
		if(!logPath.endsWith("\\")){
			logPath+="\\";
		}
		this.logPath = logPath;
	}
	private void checkFileDir(String path){
		File file = new File(path);
		if(!file.exists()){
			file.mkdirs();
		}
	}
	
	private String getDefaultLogName(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd",Locale.CHINA);
		String dateString = sdf.format(new Date());
		Random random = new Random(System.currentTimeMillis());
		StringBuffer sBuffer = new StringBuffer();
		for(int i=0;i<5;i++){
			sBuffer.append(random.nextInt(10));
		}
		return dateString+" "+sBuffer.toString()+".txt";
	}
	
	private String getDefaultLogFilePath(){
		if(logName==null){
			logName = getDefaultLogName();
		}
		return logPath+logName;
	}
	
	@Override
	public synchronized void log(Level level, String info) {
		// TODO Auto-generated method stub
		String logFilePath = getDefaultLogFilePath();
		StringBuffer sBuffer = new StringBuffer(1024);
		sBuffer.append(getCurrentTimeStamp());
		sBuffer.append("\t"+level+"\t"+info);
		File file = new File(logFilePath);
		try {
			if (!file.exists()) {
				file.createNewFile();
			}
			FileWriter fileWriter = new FileWriter(file, true);
			BufferedWriter bWriter = new BufferedWriter(fileWriter);
			bWriter.write(sBuffer.toString());
			bWriter.newLine();
			bWriter.flush();
			bWriter.close();
		} catch (IOException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	private String getCurrentTimeStamp(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss",Locale.CHINA);
		return sdf.format(new Date());
	}
	@Override
	public void info(String info) {
		// TODO Auto-generated method stub
		log(Level.info, info);
	}
	@Override
	public void debug(String info) {
		// TODO Auto-generated method stub
		log(Level.debug, info);
	}
	@Override
	public void warinig(String info) {
		// TODO Auto-generated method stub
		log(Level.warning, info);
	}
	@Override
	public void error(String info) {
		// TODO Auto-generated method stub
		log(Level.error, info);
	}
}
相關文章
相關標籤/搜索