面试必备100道经典Java基础题.docx

上传人:b****0 文档编号:12584423 上传时间:2023-04-20 格式:DOCX 页数:127 大小:55.58KB
下载 相关 举报
面试必备100道经典Java基础题.docx_第1页
第1页 / 共127页
面试必备100道经典Java基础题.docx_第2页
第2页 / 共127页
面试必备100道经典Java基础题.docx_第3页
第3页 / 共127页
面试必备100道经典Java基础题.docx_第4页
第4页 / 共127页
面试必备100道经典Java基础题.docx_第5页
第5页 / 共127页
点击查看更多>>
下载资源
资源描述

面试必备100道经典Java基础题.docx

《面试必备100道经典Java基础题.docx》由会员分享,可在线阅读,更多相关《面试必备100道经典Java基础题.docx(127页珍藏版)》请在冰豆网上搜索。

面试必备100道经典Java基础题.docx

面试必备100道经典Java基础题

面试必备100道经典Java基础题

1.完成数组int[]a={100,40,60,87,34,11,56,0}的快速排序、冒泡排序;

快速排序

实现代码:

publicclassTest001{

   publicstaticvoidmain(String[]args){

      int[]a=newint[]{100,40,60,87,34,11,56,0};

      System.out.println("未排序之前的数据是:

");

      print(a);

      System.out.println("排序之后的数据是:

");

      sort(a,0,a.length-1);

      print(a);

      

   }

   //打印方法

   publicstaticvoidprint(int[]b){

      for(inti=0;i

          System.out.print(b[i]+"");

      }

      System.out.println();

   }

   //排序方法

   staticvoidsort(int[]a,intlow,inthigh){

      if(low>=high)return;//low小于high,则直接返回

      if((high-low)==1){//如果只有两个数字,则直接比较

          if(a[0]>a[1])

             s);

          return;

      }

      intpivot=a[low];//取第一个数作为中间数

      intleft=low+1;

      intright=high;

      while(left

          //从左边开始找

          while(left

             if(a[left]>pivot)break;

             left++;//左下标往右边走一点

          }

          //从右边开始找

          while(left<=right&&right>low){//如果左大于右则一直循环

             if(a[right]<=pivot)

                 break;

             right--;//右下标往左走一点

          }

          if(left

             s);

      }

      s);

      sort(a,low,right);

      sort(a,right+1,high);

   }

   //调位方法

   privatestaticvoids[]array,inti,intj){

      inttemp;

      temp=array[i];

      array[i]=array[j];

      array[j]=temp;

   }

}

打印结果为:

未排序之前的数据是:

1004060873411560

排序之后的数据是:

0113440566087100

 

冒泡排序

实现代码:

publicclassTest002{

   publicstaticvoidmain(String[]args){

      int[]arr={100,40,60,87,34,11,56,0};//定义数组

      System.out.println("未排序之前的数据是:

");

      maopaoPrint(arr);

      System.out.println();

      System.out.println("排序之后的数据是:

");

      maopaoSort(arr);

   }

   //排序方法

   publicstaticvoidmaopaoSort(int[]arrys){

      //定义临时变量temp

      inttemp=0;

      //用j表示下标,遍历数组

      for(intj=0;j

          //对于每一个数组元素,从0到还未排序的最大下标,总是把最大的数字放在后边

          for(intk=0;k

             if(arrys[k]>arrys[k+1]){//判断当前数字与后面数字的大小

                 temp=arrys[k];

                 arrys[k]=arrys[k+1];

                 arrys[k+1]=temp;

             }

          }

      }

      maopaoPrint(arrys);//打印输出

   }

   //打印方法

   publicstaticvoidmaopaoPrint(int[]l){

      for(inti=0;i

          System.out.print(l[i]+"");//从小到大的输出

      }

   }

}

打印结果为:

未排序之前的数据是:

1004060873411560

排序之后的数据是:

0113440566087100

 

2.采用折半查找的算法,在数组中查询到某个数;

实现代码:

importjava.util.Scanner;

publicclassTest003{

   publicstaticintMax=20;

   //数据数组源

   publicstaticintdata[]={12,16,19,22,25,32,39,39,48,55,57,58,

          63,68,69,70,78,84,88,90,97};

   //计数器

   publicstaticintcount=1;

   publicstaticvoidmain(String[]args){

      System.out.println("请输入您要查找的数字:

");

      Scannersc=newScanner(System.in);

      intKeyValue=sc.nextInt();

      //调用折半查找

      if(Search(KeyValue)){

          //输出查找次数

          System.out.println("共查找了"+count+"次");

      }else{

          //输出没有找到数据

          System.out.println("抱歉,数据数组源中找不到您输入的数字");

      }

   }

   //折半查找法

   publicstaticbooleanSearch(intk){

      intleft=0;//左边界变量

      intright=Max-1;//右边界变量

      intmiddle;//中位数变量

      while(left<=right){

          middle=(left+right)/2;

          if(k

             right=middle-1;//查找前半段

          }elseif(k>data[middle]){

             left=middle+1;//查找后半段

          }elseif(k==data[middle]){

             System.out.println("Data["+middle+"]="+data[middle]);

             returntrue;

          }

          count++;

      }

      returnfalse;

   }

}

3.输入一个字符串,其中包含数字、特殊符号(像:

¥、&、(、>等)、大小写字母等,然后输出每个字符串或符号的ASCII码的和;例如:

输入“@#$%^&*():

"|”,则打印出643。

实现代码:

publicclassTest001{

   publicstaticvoidmain(String[]args){

      System.out.println("请输入一个字符串:

");

      Scannersc=newScanner(System.in);

      Stringstr=sc.nextLine();

      intsum=0;

      for(inti=0;i

          sum=sum+str.charAt(i);

      }

      System.out.println("您输入的字符串每个字节相加的和为:

"+sum);

   }

}

 

4.将一个数组中值=0的项去掉,将不为0的值存入一个新的数组

比如:

inta[]={0,1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5};

生成的新数组为:

intb[]={1,3,4,5,6,6,5,4,7,6,7,5}

实现代码:

importjava.util.*;

publicclassTest001{

   publicstaticvoidmain(String[]args){

      inta[]={0,1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5};

      Listlist=newArrayList();

      for(inti=0;i

          if(a[i]!

=0){

             list.add(a[i]);

          }

      }

      intb[]=newint[list.size()];

      for(inti=0;i

          b[i]=list.get(i);

      }

      System.out.println("原数组为:

");

      for(inti=0;i

          System.out.print(a[i]+"");

      }

      System.out.println();

      System.out.println("去掉值为0的项之后为:

");

      for(inti:

b){

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

      }

   }

}

 

5.定义10个长度的Student数组,将10个Student对象的年龄全部加1,然后把10个Student对象的详细信息逐行打印出来(数组和ArrayList实现)

实现代码:

第一个类:

publicclassStudent{

   publicStringname;

   publicStringsex;

   publicintage;

   

   publicStringgetName(){

      returnname;

   }

   publicvoidsetName(Stringname){

      this.name=name;

   }

   publicStringgetSex(){

      returnsex;

   }

   publicvoidsetSex(Stringsex){

      this.sex=sex;

   }

   publicintgetAge(){

      returnage;

   }

   publicvoidsetAge(intage){

      this.age=age;

   }

   publicStudent(Stringname,Stringsex,intage){

      super();

      this.name=name;

      this.sex=sex;

      this.age=age;

   }

}

第二个类:

importjava.util.ArrayList;

importjava.util.List;

publicclassTest001{

   staticStudent[]s=newStudent[10];

   intk=1;

   publicstaticvoidmain(String[]args){

      Listli=newArrayList();

      for(inti=0;i<10;i++){

          li.add(newStudent("zhangsan"+i,"男",20));

      }

      for(inti=0;i<10;i++){

          (li.get(i).age)++;

      }

      for(inti=0;i<10;i++){

          System.out.println(li.get(i).getName()+""+li.get(i).getSex()+""+li.get(i).getAge());

      }

   }

}

 

6.有工人,农民,教师,科学家,服务生,其中,工人,农民,服务生只有基本工资.教师除基本工资外,还有课酬(元/天),科学家除基本工资外,还有年终奖,请你写出相关类,将各种类型的员工的全年工资打印出来

实现代码:

(共有7个类)

第一个类:

packagecom.softeem.zy006;

/**

 *定义一个人的接口,以供实现

 */

publicinterfacePeople{

   publicdoublenum();

}

第二个类:

packagecom.softeem.zy006;

/**

 *工人类

 */

publicclassWorkerimplementsPeople{

   privatedoublemontherSalary;

   publicWorker(doublemontherSalary){

      super();

      this.montherSalary=montherSalary;

   }

   publicdoublenum(){

      returngetMontherSalary()*12;

   }

   publicdoublegetMontherSalary(){

      returnmontherSalary;

   }

   publicvoidsetMontherSalary(doublemontherSalary){

      this.montherSalary=montherSalary;

   }

}

第三个类:

packagecom.softeem.zy006;

/**

 *农民类

 */

publicclassPeasantimplementsPeople{

   privatedoublemontherSalary;

   publicPeasant(doublemontherSalary){

      super();

      this.montherSalary=montherSalary;

   }

   publicdoublegetMontherSalary(){

      returnmontherSalary;

   }

   publicvoidsetMontherSalary(doublemontherSalary){

      this.montherSalary=montherSalary;

   }

   publicdoublenum(){

      returngetMontherSalary()*12;

   }

}

第四个类:

packagecom.softeem.zy006;

/**

 *教师类

 */

publicclassTeacherimplementsPeople{

   privatedoublemontherSalary;

   privatedoubledaySalary;

   

   publicTeacher(doublemontherSalary,doubledaySalary){

      super();

      this.montherSalary=montherSalary;

      this.daySalary=daySalary;

   }

   publicdoublenum(){

      returngetMontherSalary()*12+getDaySalary()*365;

   }

   publicdoublegetMontherSalary(){

      returnmontherSalary;

   }

   publicvoidsetMontherSalary(doublemontherSalary){

      this.montherSalary=montherSalary;

   }

   publicdoublegetDaySalary(){

      returndaySalary;

   }

   publicvoidsetDaySalary(doubledaySalary){

      this.daySalary=daySalary;

   }

}

第五个类:

packagecom.softeem.zy006;

/**

 *科学家类

 */

publicclassScientistimplementsPeople{

   privatedoublemontherSalary;

   privatedoubleprojectSalary;

   publicScientist(doublemontherSalary,doubleprojectSalary){

      super();

      this.montherSalary=montherSalary;

      this.projectSalary=projectSalary;

   }

   publicdoublenum(){

      returngetMontherSalary()*12+getProjectSalary();

   }

   publicdoublegetMontherSalary(){

      returnmontherSalary;

   }

   publicvoidsetMontherSalary(doublemontherSalary){

      this.montherSalary=montherSalary;

   }

   publicdoublegetProjectSalary(){

      returnprojectSalary;

   }

   publicvoidsetProjectSalary(doubleprojectSalary){

      this.projectSalary=projectSalary;

   }

}

第六个类:

packagecom.softeem.zy006;

/**

 *服务员类

 */

publicclassWaiterimplementsPeople{

   privatedoublemontherSalary;

   publicWaiter(doublemontherSalary){

      super();

      this.montherSalary=montherSalary;

   }

   publicdoublenum(){

      returngetMontherSalary()*12;

   }

   publicdoublegetMontherSalary(){

      returnmontherSalary;

   }

   publicvoid

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 人文社科 > 哲学历史

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1