java面试重要手写体

1、ArrayList如何实现插入的数据按自定义的方式有序存放
class MyBean implements Comparable{
 public int compareTo(Object obj){
  if(! obj instanceof MyBean)
   throw new ClassCastException() //具体异常的名称,我要查jdk文档。
   
  MyBean other = (MyBean) obj;
  return age > other.age?1:age== other.age?0:-1;
 }
}


class MyTreeSet {

 private ArrayList  datas = new ArrayList();
 
 public void add(Object obj){
  for(int i=0;i<datas.size();i++){
   if(obj.compareTo(datas.get(i) != 1){
    datas.add(i,obj);
   }
  }
 }
}

2、编写一个函数将一个十六进制数的字符串参数转换成整数返回。
 String str = “13abf”;
 int len = str.length;
 int sum = 0;
 for(int i=0;i<len;i++){
  char c = str.charAt(len-1-i);
  int n = Character.digit(c,16);
  sum += n * (1<<(4*i)); 
 }


3、设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1。写出程序。
以下程序使用内部类实现线程,对j增减的时候没有考虑顺序问题。
public class ThreadTest1
{
private int j;
public static void main(String args[]){
   ThreadTest1 tt=new ThreadTest1();
   Inc inc=tt.new Inc();
   Dec dec=tt.new Dec();
   for(int i=0;i<2;i++){
       Thread t=new Thread(inc);
       t.start();
     t=new Thread(dec);
       t.start();
       }
   }
private synchronized void inc(){
   j++;
   System.out.println(Thread.currentThread().getName()+"-inc:"+j);
   }
private synchronized void dec(){
   j--;
   System.out.println(Thread.currentThread().getName()+"-dec:"+j);
   }
class Inc implements Runnable{
   public void run(){
       for(int i=0;i<100;i++){
       inc();
       }
   }
}
class Dec implements Runnable{
   public void run(){
       for(int i=0;i<100;i++){
       dec();
       }
   }
}
}

----------随手再写的一个-------------
class A
{
JManger j =new JManager();
main()
{
 new A().call();
}

void call
{
 for(int i=0;i<2;i++)
 {
  new Thread(
   new Runnable(){ public void run(){while(true){j.accumulate()}}}
  ).start();
  new Thread(new Runnable(){ public void run(){while(true){j.sub()}}}).start();
 }
}
}

class JManager
{
 private j = 0;
 
 public synchronized void subtract()
 {
  j--
 }
 
 public synchronized void accumulate()
 {
  j++;
 }
 
}

 

 

4、子线程循环10次,接着主线程循环100,接着又回到子线程循环10次,接着再回到主线程又循环100,如此循环50次,请写出程序。

最终的程序代码如下:
public class ThreadTest {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  new ThreadTest().init();

 }

 public void init()
 {
  final Business business = new Business();
  new Thread(
    new Runnable()
    {

     public void run() {
      for(int i=0;i<50;i++)
      {
       business.SubThread(i);
      }      
     }
     
    }
  
  ).start();
  
  for(int i=0;i<50;i++)
  {
   business.MainThread(i);
  }  
 }
 
 private class Business
 {
  boolean bShouldSub = true;//这里相当于定义了控制该谁执行的一个信号灯
  public synchronized void MainThread(int i)
  {
   if(bShouldSub)
    try {
     this.wait();
    } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }  
    
   for(int j=0;j<5;j++)
   {
    System.out.println(Thread.currentThread().getName() + ":i=" + i +",j=" + j);
   }
   bShouldSub = true;
   this.notify();
  
  }
  
  
  public synchronized void SubThread(int i)
  {
   if(!bShouldSub)
    try {
     this.wait();
    } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    } 
    
   for(int j=0;j<10;j++)
   {
    System.out.println(Thread.currentThread().getName() + ":i=" + i +",j=" + j);
   }
   bShouldSub = false;    
   this.notify();   
  }
 }
}


5、描述一下JVM加载class文件的原理机制?
JVM中类的装载是由ClassLoader和它的子类来实现的,Java ClassLoader 是一个重要的Java运行时系统组件。它负责在运行时查找和装入类文件的类。

 

6、java中会存在内存泄漏吗,请简单描述。
所谓内存泄露就是指一个不再被程序使用的对象或变量一直被占据在内存中。java中有垃圾回收机制,它可以保证一对象不再被引用的时候,即对象编程了孤儿的时候,对象将自动被垃圾回收器从内存中清除掉。由于Java 使用有向图的方式进行垃圾回收管理,可以消除引用循环的问题,例如有两个对象,相互引用,只要它们和根进程不可达的,那么GC也是可以回收它们的


7、编写一个程序,将a.txt文件中的单词与b.txt文件中的单词交替合并到c.txt文件中,a.txt文件中的单词用回车符分隔,b.txt文件中用回车或空格进行分隔。
答:
  package cn.itcast;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

public class MainClass{
 public static void main(String[] args) throws Exception{
  FileManager a = new FileManager("a.txt",new char[]{'\n'});
  FileManager b = new FileManager("b.txt",new char[]{'\n',' '});  
  FileWriter c = new FileWriter("c.txt");
  String aWord = null;
  String bWord = null;
  while((aWord = a.nextWord()) !=null ){
   c.write(aWord + "\n");
   bWord = b.nextWord();
   if(bWord != null)
    c.write(bWord + "\n");
  }
  
  while((bWord = b.nextWord()) != null){
   c.write(bWord + "\n");
  } 
  c.close();
 }
 
}


class FileManager{

 String[] words = null;
 int pos = 0;
 public FileManager(String filename,char[] seperators) throws Exception{
  File f = new File(filename);
  FileReader reader = new FileReader(f);
  char[] buf = new char[(int)f.length()];
  int len = reader.read(buf);
  String results = new String(buf,0,len);
  String regex = null;
  if(seperators.length >1 ){
   regex = "" + seperators[0] + "|" + seperators[1];
  }else{
   regex = "" + seperators[0];
  }
  words = results.split(regex);
 }
 
 public String nextWord(){
  if(pos == words.length)
   return null;
  return words[pos++];
 }

}


8、编写一个程序,将d:\java目录下的所有.java文件复制到d:\jad目录下,并将原来文件的扩展名从.java改为.j
答:listFiles方法接受一个FileFilter对象,这个FileFilter对象就是过虑的策略对象,不同的人提供不同的FileFilter实现,即提供了不同的过滤策略。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Jad2Java {

 public static void main(String[] args) throws Exception {
  File srcDir = new File("java");
  if(!(srcDir.exists() && srcDir.isDirectory()))
    throw new Exception("目录不存在");
  File[] files = srcDir.listFiles(
   new FilenameFilter(){

     public boolean accept(File dir, String name) {
      return name.endsWith(".java");
     }
     
    }
  );
  
  System.out.println(files.length);
  File destDir = new File("jad");
  if(!destDir.exists()) destDir.mkdir();
  for(File f :files){
   FileInputStream  fis = new FileInputStream(f);
   String destFileName = f.getName().replaceAll("
\\.java$", ".jad");
   FileOutputStream fos = new FileOutputStream(new File(destDir,destFileName));
   copy(fis,fos);
   fis.close();
   fos.close();
  }
 }
 
 private static void copy(InputStream ips,OutputStream ops) throws Exception{
  int len = 0;
  byte[] buf = new byte[1024];
  while((len = ips.read(buf)) != -1){
   ops.write(buf,0,len);
  }

 }
}


9、编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串,但要保证汉字不被截取半个,如“我ABC”,4,应该截取“我AB”,输入“我ABC汉DEF”,6,应该输出“我ABC”,而不是“我ABC+汉的半个”。
答:
 首先要了解中文字符有多种编码及各种编码的特征。
    假设n为要截取的字节数。
 public static void main(String[] args) throws Exception{
  String str = "我a爱中华abc我爱传智def';
  String str = "我ABC汉";
  int num = trimGBK(str.getBytes("GBK"),5);
  System.out.println(str.substring(0,num) );
 }
 
 public static int  trimGBK(byte[] buf,int n){
  int num = 0;
  boolean bChineseFirstHalf = false;
  for(int i=0;i<n;i++)
  {
   if(buf[i]<0 && !bChineseFirstHalf){
    bChineseFirstHalf = true;
   }else{
    num++;
    bChineseFirstHalf = false;    
   }
  }
  return num;
 }

 

10、有一个字符串,其中包含中文字符、英文字符和数字字符,请统计和打印出各个字符的个数。
答:哈哈,其实包含中文字符、英文字符、数字字符原来是出题者放的烟雾弹。
String content = “中国aadf的111萨bbb菲的zz萨菲”;
HashMap map = new HashMap();
for(int i=0;i<content.length;i++)
{
 char c = content.charAt(i);
 Integer num = map.get(c);
 if(num == null)
  num = 1;
 else
  num = num + 1;
 map.put(c,num);
}
for(Map.EntrySet entry : map)
{
 system.out.println(entry.getkey() + “:” + entry.getValue());
}
估计是当初面试的那个学员表述不清楚,问题很可能是:
如果一串字符如"aaaabbc中国1512"要分别统计英文字符的数量,中文字符的数量,和数字字符的数量,假设字符中没有中文字符、英文字符、数字字符之外的其他特殊字符。
int engishCount;
int chineseCount;
int digitCount;
for(int i=0;i<str.length;i++)
{
 char ch = str.charAt(i);
 if(ch>=’0’ && ch<=’9’)
 {
  digitCount++
 }
 else if((ch>=’a’ && ch<=’z’) || (ch>=’A’ && ch<=’Z’))
 {
  engishCount++;
 }
 else
 {
  chineseCount++;
 }
}
System.out.println(……………);


11、写一个Singleton出来。
第一种:饱汉模式
public class SingleTon {
 private SingleTon(){
  }

 //实例化放在静态代码块里可提高程序的执行效率,但也可能更占用空间 
 private final static SingleTon instance = new SingleTon();
 public static SingleTon getInstance(){
  return instance;
 }
}

第二种:饥汉模式
public class SingleTon {
 private SingleTon(){}
 
 private static instance = null;//new SingleTon();
 
 public static synchronized SingleTon getInstance(){
  if(instance == null)
   instance = new SingleTon();
  return instance;
 }
}

第三种:用枚举
 public enum SingleTon{
  ONE;
 
 }


12、第1个人10,第2个比第1个人大2岁,依次递推,请用递归方式计算出第8个人多大?
package cn.itcast;

import java.util.Date;

public class A1 {

 public static void main(String [] args)
 {
  System.out.println(computeAge(8));
 }
 
 public static int computeAge(int n)
 {
  if(n==1) return 10;
  return computeAge(n-1) + 2;
 }
}

 public static void toBinary(int n,StringBuffer result)
 {

  if(n/2 != 0)
   toBinary(n/2,result);
  result.append(n%2);  
 }

 

13、排序都有哪几种方法?请列举。用JAVA实现一个快速排序。
 本人只研究过冒泡排序、选择排序和快速排序,下面是快速排序的代码:
public class QuickSort {
/**
* 快速排序
* @param strDate
* @param left
* @param right
*/
public void quickSort(String[] strDate,int left,int right){
String middle,tempDate;
int i,j;
i=left;
j=right;
middle=strDate[(i+j)/2];
do{
while(strDate[i].compareTo(middle)<0&& i<right)
i++; //找出左边比中间值大的数
while(strDate[j].compareTo(middle)>0&& j>left)
j--; //找出右边比中间值小的数
if(i<=j){ //将左边大的数和右边小的数进行替换
tempDate=strDate[i];
strDate[i]=strDate[j];
strDate[j]=tempDate;
i++;
j--;
}
}while(i<=j); //当两者交错时停止

if(i<right){
quickSort(strDate,i,right);//从
}
if(j>left){
quickSort(strDate,left,j);
}
}
/**
  * @param args
  */
public static void main(String[] args){
String[] strVoid=new String[]{"11","66","22","0","55","22","0","32"};
QuickSort sort=new QuickSort();
sort.quickSort(strVoid,0,strVoid.length-1);
for(int i=0;i<strVoid.length;i++){
System.out.println(strVoid[i]+" ");
}
}


}


14、分页语句
取出sql表中第31到40的记录(以自动增长ID为主键)
sql server方案1:
 select top 10 * from t where id not in (select top 30 id from t order by id ) orde by id
sql server方案2:
 select top 10 * from t where id in (select top 40 id from t order by id) order by id desc

mysql方案:select * from t order by id limit 30,10

oracle方案:select * from (select rownum r,* from t where r<=40) where r>30

--------------------待整理进去的内容-------------------------------------
pageSize=20;
pageNo = 5;

1.分页技术1(直接利用sql语句进行分页,效率最高和最推荐的)

mysql:sql = "select * from articles limit " + (pageNo-1)*pageSize + "," + pageSize;
oracle: sql = "select * from " +
        "(select rownum r,* from " +
         "(select * from articles order by postime desc)" +
        "where rownum<= " + pageNo*pageSize +") tmp " +
       "where r>" + (pageNo-1)*pageSize;

       

       
       
15、.用一条SQL语句 查询出每门课都大于80分的学生姓名 
name   kecheng   fenshu
张三     语文       81
张三     数学       75
李四     语文       76
李四     数学       90
王五     语文       81
王五     数学       100
王五     英语       90

准备数据的sql代码:
create table score(id int primary key auto_increment,name varchar(20),subject varchar(20),score int);
insert into score values
(null,'张三','语文',81),
(null,'张三','数学',75),
(null,'李四','语文',76),
(null,'李四','数学',90),
(null,'王五','语文',81),
(null,'王五','数学',100),
(null,'王五 ','英语',90);

提示:当百思不得其解时,请理想思维,把小变成大做,把大变成小做,

答案:
A: select distinct name from score  where  name not in (select distinct name from score where score<=80)

B:select distince name t1 from score where 80< all (select score from score where name=t1);


16、用JDBC如何调用存储过程
代码如下:
package com.huawei.interview.lym;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Types;

public class JdbcTest {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Connection cn = null;
  CallableStatement cstmt = null;  
  try {
   //这里最好不要这么干,因为驱动名写死在程序中了
   Class.forName("com.mysql.jdbc.Driver");
   //实际项目中,这里应用DataSource数据,如果用框架,
   //这个数据源不需要我们编码创建,我们只需Datasource ds = context.lookup()
   //cn = ds.getConnection();   
   cn = DriverManager.getConnection("jdbc:mysql:///test","root","root");
   cstmt = cn.prepareCall("{call insert_Student(?,?,?)}");
   cstmt.registerOutParameter(3,Types.INTEGER);
   cstmt.setString(1, "wangwu");
   cstmt.setInt(2, 25);
   cstmt.execute();
   //get第几个,不同的数据库不一样,建议不写
   System.out.println(cstmt.getString(3));
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  finally
  {

   /*try{cstmt.close();}catch(Exception e){}
   try{cn.close();}catch(Exception e){}*/
   try {
    if(cstmt != null)
     cstmt.close();
    if(cn != null)    
     cn.close();
   } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }

 
 17、用 JDBC 查询学生成绩单, 把主要代码写出来(考试概率极大).
Connection cn = null;
PreparedStatement pstmt =null;
Resultset rs = null;
try
{
 Class.forname(driveClassName);
 cn =  DriverManager.getConnection(url,username,password);
 pstmt = cn.prepareStatement(“select  score.* from score ,student “ +
  “where score.stuId = student.id and student.name = ?”);
 pstmt.setString(1,studentName);
 Resultset rs = pstmt.executeQuery();
 while(rs.next())
 {
  system.out.println(rs.getInt(“subject”)  +  “    ” + rs.getFloat(“score”) );
 }
}catch(Exception e){e.printStackTrace();}
finally
{
 if(rs != null) try{ rs.close() }catch(exception e){}
 if(pstmt != null) try{pstmt.close()}catch(exception e){}
 if(cn != null) try{ cn.close() }catch(exception e){}
}


18、说出数据连接池的工作机制是什么?
J2EE服务器启动时会建立一定数量的池连接,并一直维持不少于此数目的池连接。客户端程序需要连接时,池驱动程序会返回一个未使用的池连接并将其表记为忙。如果当前没有空闲连接,池驱动程序就新建一定数量的连接,新建连接的数量有配置参数决定。当使用的池连接调用完成后,池驱动程序将此连接表记为空闲,其他调用就可以使用这个连接。
实现方式,返回的Connection是原始Connection的代理,代理Connection的close方法不是真正关连接,而是把它代理的Connection对象还回到连接池中。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值