面试必备100道经典Java基础题_第1页
面试必备100道经典Java基础题_第2页
面试必备100道经典Java基础题_第3页
面试必备100道经典Java基础题_第4页
面试必备100道经典Java基础题_第5页
已阅读5页,还剩84页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

面试必备面试必备 100 道经典道经典 Java 基础题基础题 1 完成数组完成数组 int a 100 40 60 87 34 11 56 0 的快速排序 冒泡排序 的快速排序 冒泡排序 快速排序快速排序 实现代码 public class Test001 public static void main String args int a new int 100 40 60 87 34 11 56 0 System out println 未排序之前的数据是 print a System out println 排序之后的数据是 sort a 0 a length 1 print a 打印方法 public static void print int b for int i 0 i high return low 小于 high 则直接返回 if high low 1 如果只有两个数字 则直接比较 if a 0 a 1 swap a 0 1 return int pivot a low 取第一个数作为中间数 int left low 1 int right high while left right 从左边开始找 while left right left 左下标往右边走一点 从右边开始找 while left low 如果左大于右则一直循环 if a right pivot break right 右下标往左走一点 if left right 如果还没有找完 则交换数字 swap a right left swap a low right sort a low right sort a right 1 high 调位方法 private static void swap int array int i int j int temp temp array i array i array j array j temp 打印结果为 未排序之前的数据是 100 40 60 87 34 11 56 0 排序之后的数据是 0 11 34 40 56 60 87 100 冒泡排序冒泡排序 实现代码 public class Test002 public static void main 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 排序方法 public static void maopaoSort int arrys 定义临时变量 temp int temp 0 用 j 表示下标 遍历数组 for int j 0 j arrys length j 对于每一个数组元素 从 0 到还未排序的最大下标 总是把最大的数字放在后边 for int k 0 karrys k 1 判断当前数字与后面数字的大小 temp arrys k arrys k arrys k 1 arrys k 1 temp maopaoPrint arrys 打印输出 打印方法 public static void maopaoPrint int l for int i 0 i l length i System out print l i 从小到大的输出 打印结果为 未排序之前的数据是 100 40 60 87 34 11 56 0 排序之后的数据是 0 11 34 40 56 60 87 100 2 采用采用折半查找折半查找的算法 在数组中查询的算法 在数组中查询 到某个数 到某个数 实现代码 import java util Scanner public class Test003 public static int Max 20 数据数组源 public static int data 12 16 19 22 25 32 39 39 48 55 57 58 63 68 69 70 78 84 88 90 97 计数器 public static int count 1 public static void main String args System out println 请输入您要查找的数字 Scanner sc new Scanner System in int KeyValue sc nextInt 调用折半查找 if Search KeyValue 输出查找次数 System out println 共查找了 count 次 else 输出没有找到数据 System out println 抱歉 数据数组源中找不到您输入的数字 折半查找法 public static boolean Search int k int left 0 左边界变量 int right Max 1 右边界变量 int middle 中位数变量 while left right middle left right 2 if k data middle left middle 1 查找后半段 else if k data middle System out println Data middle data middle return true count return false 3 输入一个字符串 其中包含数字 特输入一个字符串 其中包含数字 特 殊符号殊符号 像像 Scanner sc new Scanner System in String str sc nextLine int sum 0 for int i 0 i str length i sum sum str charAt i System out println 您输入的字符串每个字节相加的和为 sum 4 将一个数组中值将一个数组中值 0 的项去掉的项去掉 将不为将不为 0 的值存入一个新的数组的值存入一个新的数组 比如 int a 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 实现代码 import java util public class Test001 public static void main String args int a 0 1 3 4 5 0 0 6 6 0 5 4 7 6 7 0 5 List list new ArrayList for int i 0 i a length i if a i 0 list add a i int b new int list size for int i 0 i list size i b i list get i System out println 原数组为 for int i 0 i a length i System out print a i System out println System out println 去掉值为 0 的项之后为 for int i b System out print i 5 定义定义 10 个长度的个长度的 Student 数组 将数组 将 10 个个 Student 对象的年龄全部加对象的年龄全部加 1 然后把然后把 10 个个 Student 对象的详细信息对象的详细信息 逐行打印出来逐行打印出来 数组和数组和 ArrayList 实现实现 实现代码 第一个类 public class Student public String name public String sex public int age public String getName return name public void setName String name this name name public String getSex return sex public void setSex String sex this sex sex public int getAge return age public void setAge int age this age age public Student String name String sex int age super this name name this sex sex this age age 第二个类 import java util ArrayList import java util List public class Test001 static Student s new Student 10 int k 1 public static void main String args List li new ArrayList for int i 0 i 10 i li add new Student zhangsan i 男 20 for int i 0 i 10 i li get i age for int i 0 i 10 i System out println li get i getName li get i getSex li get i getAge 6 有工人有工人 农民农民 教师教师 科学家科学家 服务生服务生 其其 中中 工人工人 农民农民 服务生只有基本工资服务生只有基本工资 教师教师 除基本工资外除基本工资外 还有课酬还有课酬 元元 天天 科学家科学家 除基本工资外除基本工资外 还有年终奖还有年终奖 请你写出相请你写出相 关类关类 将各种类型的员工的全年工资打印将各种类型的员工的全年工资打印 出来出来 实现代码 共有 7 个类 第一个类 package com softeem zy006 定义一个人的接口 以供实现 public interface People public double num 第二个类 package com softeem zy006 工人类 public class Worker implements People private double montherSalary public Worker double montherSalary super this montherSalary montherSalary public double num return getMontherSalary 12 public double getMontherSalary return montherSalary public void setMontherSalary double montherSalary this montherSalary montherSalary 第三个类 package com softeem zy006 农民类 public class Peasant implements People private double montherSalary public Peasant double montherSalary super this montherSalary montherSalary public double getMontherSalary return montherSalary public void setMontherSalary double montherSalary this montherSalary montherSalary public double num return getMontherSalary 12 第四个类 package com softeem zy006 教师类 public class Teacher implements People private double montherSalary private double daySalary public Teacher double montherSalary double daySalary super this montherSalary montherSalary this daySalary daySalary public double num return getMontherSalary 12 getDaySalary 365 public double getMontherSalary return montherSalary public void setMontherSalary double montherSalary this montherSalary montherSalary public double getDaySalary return daySalary public void setDaySalary double daySalary this daySalary daySalary 第五个类 package com softeem zy006 科学家类 public class Scientist implements People private double montherSalary private double projectSalary public Scientist double montherSalary double projectSalary super this montherSalary montherSalary this projectSalary projectSalary public double num return getMontherSalary 12 getProjectSalary public double getMontherSalary return montherSalary public void setMontherSalary double montherSalary this montherSalary montherSalary public double getProjectSalary return projectSalary public void setProjectSalary double projectSalary this projectSalary projectSalary 第六个类 package com softeem zy006 服务员类 public class Waiter implements People private double montherSalary public Waiter double montherSalary super this montherSalary montherSalary public double num return getMontherSalary 12 public double getMontherSalary return montherSalary public void setMontherSalary double montherSalary this montherSalary montherSalary 第七个类 package com softeem zy006 测试类 public class Test public static void main String args Test a new Test Worker w new Worker 1000 System out println 工人的年薪为 w num 元 Peasant p new Peasant 2500 System out println 农民的年薪为 p num 元 Teacher t new Teacher 4500 50 System out println 教师的年薪为 t num 元 Scientist e new Scientist 10500 30000 System out println 科学家的年薪为 e num 元 Waiter y new Waiter 3400 System out println 服务生的年薪为 y num 打印结果为 工人的年薪为 12000 0 元 农民的年薪为 30000 0 元 教师的年薪为 72250 0 元 科学家的年薪为 156000 0 元 服务生的年薪为 40800 0 7 创建一个复数类创建一个复数类 complex 对复数 对复数 进行数学运算 复数具有如下格式 进行数学运算 复数具有如下格式 RealPart ImaginaryPart I 其中 I 为 的平方根 要求如下 利用浮点变量表示此类的私有数据 提供两个构造方法 一个用于此类声明时对象 的初始化 一个为带默认值得无参构造方法 提供两复数加 减 乘的运算方法 按格式 a b 打印复数 其中 a 为实部 b 为虚部 实现代码 package com softeem zy007 创建一个复数类 complex 对复数进行数学运算 复数具有如下格式 RealPart ImaginaryPart I 其 中 I 为 的平方根 要求如下 利用浮点变量表示此类的私有数据 提供两个构造方法 一个用于此类声明时对象的 初始化 一个为带默认值得无参构造方法 提供两复数加 减 乘的运算方法 按格式 a b 打印复数 其中 a 为实部 b 为虚部 public class ComplexNumber implements Cloneable 复数的实部 private double realPart 复数的虚部 private double imaginaryPart 默认构造函数 public ComplexNumber this realPart 0 0 this imaginaryPart 0 0 构造函数 param a 实部 param b 虚部 public ComplexNumber double a double b this realPart a this imaginaryPart b 复数的加法运算 c a b 的运算法则是 c 实部 a 实部 b 实部 c 虚部 a 虚部 b 虚部 param aComNum 加数 return public ComplexNumber add ComplexNumber aComNum if aComNum null System err println 对象不能够为 null return new ComplexNumber return new ComplexNumber this realPart aComNum getRealPart this imaginaryPart aComNum getImaginaryPart 复数的减法运算 c a b 的运算法则是 c 实部 a 实部 b 实部 c 虚部 a 虚部 b 虚部 param aComNum 减数 return public ComplexNumber decrease ComplexNumber aComNum if aComNum null System err println 对象不能够为 null return new ComplexNumber return new ComplexNumber this realPart aComNum getRealPart this imaginaryPart aComNum getImaginaryPart 复数的乘法运算 c a b 的运算法则是 c 实部 a 实部 b 实部 a 虚部 b 虚部 c 虚部 a 虚部 b 实部 a 实部 b 虚部 param aComNum 乘数 return public ComplexNumber multiply ComplexNumber aComNum if aComNum null System err println 对象不能够为 null return new ComplexNumber double newReal this realPart aComNum realPart this imaginaryPart aComNum imaginaryPart double newImaginary this realPart aComNum imaginaryPart this imaginaryPart aComNum realPart ComplexNumber result new ComplexNumber newReal newImaginary return result 复数的除法运算 c a b 的运算法则是 c 实部 a 实部 b 实部 a 虚部 b 虚部 b 实部 b 实部 b 虚部 b 虚部 c 虚部 a 虚部 b 实部 a 实部 b 虚部 b 实部 b 实部 b 虚部 b 虚部 param aComNum 除数 return public ComplexNumber divide ComplexNumber aComNum if aComNum null System err println 对象不能够为 null return new ComplexNumber if aComNum getRealPart 0 return new ComplexNumber double temp aComNum getRealPart aComNum getRealPart aComNum getImaginaryPart aComNum getImaginaryPart double crealpart this realPart aComNum getRealPart this imaginaryPart aComNum getImaginaryPart temp double cimaginaryPart this imaginaryPart aComNum getRealPart this realPart aComNum getImaginaryPart temp return new ComplexNumber crealpart cimaginaryPart 将一个复数显示为字符串 public String toString return this realPart this imaginaryPart i 比较一个对象是否和这个复数对象的值相等 public boolean equals Object obj if obj null return false 首先判断 a 是不是一个复数对象 instanceof 关键字是用来判断对象的类型 if obj instanceof ComplexNumber 如果 a 是复数对象 需要将它强制类型转换成复数对象 才能调用复数类提供的方法 ComplexNumber b ComplexNumber obj if this realPart b getRealPart else return false else return false 获得该复数对象的 hashcode public int hashCode 如果两个复数对象是 equals 的 那么它们的 hashCode 也必须相同 两个值相等的复数对象通过 toString 方法得到的输出字符串是一样的 于是 可以把得到的字符串的 hashCode 当作复数对象的 hashCode return this toString hashCode 根据现有对象克隆一个新对象 public Object clone 如果你要使自定义的类能够被 clone 就必须实现 Cloneable 接口并且重写它的 clone 方法 如果你仅仅重写了 clone 方法而没有在类的声明中添加实现 Cloneable 接口 调用 clone 方法时将 会出现 CloneNotSupportedException 异常 读者可以试试 try ComplexNumber newObject ComplexNumber super clone newObject setRealPart this realPart newObject setImaginaryPart this imaginaryPart return newObject catch CloneNotSupportedException e 如果没有实现 Cloneable 接口 抛出异常 e printStackTrace return null return 返回 imaginaryPart public double getImaginaryPart return imaginaryPart param imaginaryPart 要设置的 imaginaryPart public void setImaginaryPart double imaginaryPart this imaginaryPart imaginaryPart return 返回 realPart public double getRealPart return realPart param realPart 要设置的 realPart public void setRealPart double realPart this realPart realPart public static void main String args throws CloneNotSupportedException ComplexNumber a new ComplexNumber 3 5 ComplexNumber b new ComplexNumber 2 4 System out println ComplexNumber a a toString System out println ComplexNumber b b toString System out println a b a add b toString System out println a b a decrease b toString System out println a b a multiply b toString System out println a b a divide b toString 8 实现圆类实现圆类 circle 包含相关的成员变 包含相关的成员变 量和成员方法 从圆类派生出圆柱类量和成员方法 从圆类派生出圆柱类 cylinder 根据建立的两个类 从键盘 根据建立的两个类 从键盘 输入输入 5 个圆的半径 个圆的半径 5 个圆柱的半径和个圆柱的半径和 高度 并分别是输出高度 并分别是输出 5 个圆的面积 个圆的面积 5 个圆柱的体积个圆柱的体积 实现代码 共有三个类 第一个类 package com softeem zy008 public class Circle private static final float f float Math PI 计算出圆的面积 public float getArea float rr float area f rr rr return area 第二个类 package com softeem zy008 public class Cylinder extends Circle public float getVolume float rr float g float area super getArea rr float volume area g return volume 第三个类 测试类 package com softeem zy008 import java util Scanner public class Test public static void main String args Test t new Test t Demo001 System out println t Demo002 public void Demo001 System out println 计算圆的面积 System out println 请输入 5 个圆的半径 一行写一个半径 float f new float 5 for int i 0 i 4 i Scanner sc new Scanner System in f i sc nextFloat for int i 0 i f length i Circle c new Circle float area c getArea f i System out println 半径为 f i 的圆的面积为 area public void Demo002 System out println 接下来是计算圆柱的体积 System out println 请先输入 5 个圆柱的半径 一行写一个 float f1 new float 5 for int i 0 i 4 i Scanner sc new Scanner System in f1 i sc nextFloat System out println 请再分别输入 5 个圆柱对应的高度 一行写一个 float f2 new float 5 for int i 0 i 4 i Scanner sc new Scanner System in f2 i sc nextFloat for int i 0 i 5 i Cylinder c new Cylinder float volume c getVolume f1 i f2 i System out println 半径为 f1 i 高为 f2 i 的圆柱的体积为 volume 9 输入一个整数 求这个整数中每位输入一个整数 求这个整数中每位 数字相加的和数字相加的和 实现代码 package com softeem zy009 import java util Scanner 输入一个整数 求这个整数中每位数字相加的和 public class Test001 public static void main String args System out println 请输入一个整数 Scanner sc new Scanner System in String s sc nextLine int sum 0 for int i 0 i2000 为为 止止 实现代码 public class Test001 public static void main String args int sum 0 for int i 1 i i 2 sum sum i System out println sum if sum 2000 break System out println 程序停止 和已经大于 2000 了 12 计算计算 s 2 4 8 11 首先先定 首先先定 义一个函数 函数的功能就是求任何一义一个函数 函数的功能就是求任何一 个数的阶乘 个数的阶乘 实现代码 public class Test001 public static void main String args Test001 t new Test001 int sum t getJieCheng 2 t getJieCheng 2 t getJieCheng 4 t getJieCheng 8 t getJieCheng 11 System out println 2 4 8 11 sum 求任何一个数的阶乘 public int getJieCheng int m int f 1 for int i 1 i m i f i f return f 13 求求 200 到到 400 间 能被 整除但不间 能被 整除但不 能被 整除的数的个数能被 整除的数的个数 实现代码 public class Test001 public static void main String args int count 0 System out println 能被 整除但不能被 整除的数有 for int i 200 i 400 i if i 3 0 count System out println System out print 能被 整除但不能被 整除的数的个数有 count 个 14 求能被求能被 3 整除且至少有一位数字为整除且至少有一位数字为 5 的三位数的个数的三位数的个数 实现代码 import java util ArrayList import java util List public class Test001 public static void main String args int count 0 List li new ArrayList for int i 100 i 999 i li add i System out println 能被 3 整除且至少有一位数字为 5 的三位数有 for int i 100 i li size i if li get i 3 0 if li get i toString substring 2 equals 5 li get i toString substring 1 2 equals 5 li get 0 toString substring 0 1 equals 5 System out print li get i count System out println r n 能被 3 整除且至少有一位数字为 5 的三位数的个数有 count 个 打印结果为 能被 3 整除且至少有一位数字为 5 的三位数有 225 252 255 258 285 315 345 351 354 357 375 405 435 450 453 456 459 465 495 525 552 555 558 585 615 645 651 654 657 675 705 735 750 753 756 759 765 795 825 852 855 858 885 915 945 951 954 957 975 能被 3 整除且至少有一位数字为 5 的三位数的个数有 49 个 15 求三位奇数中 个位数字与十位数求三位奇数中 个位数字与十位数 字之和除以字之和除以 10 所得的余数是百位数字所得的余数是百位数字 的数的个数的数的个数 实现代码 public class Test001 public static void main String args int count 0 int a 0 aa 0 aaa 0 for int i 100 i 999 i if i 2 1 a i 10 个位 aa i 10 10 十位 aaa i 10 10 百位 if aaa a aa 10 count System out println 所求个数为 count 个 16 解百马百瓦古题 解百马百瓦古题 大 小马和马驹共 100 匹 共驮 100 片瓦 大马一驮三 小马一驮二 马驹二驮一 一次 驮完 三种马都驮 共有多少种组合 实现代码 public class Test001 public static void main String args for int i 1 i 98 i for int j 1 j 98 j for int k 1 k 98 k if i j k 100 17 求求 100 200 之间的所有素数之间的所有素数 实现代码 public class Test001 public static void main String args Test001 s new Test001 s Method1 判断 101 200 之间有多少个素数 并输出所有素数 public void Method1 int count 0 System out println 100 200 之间的素数有 for int i 101 i 200 i boolean flag true for int j 2 j Math sqrt i j if i j 0 flag false break if flag count System out print i System out println n 共有 count 个 18 输出水仙花的个数输出水仙花的个数 所谓水仙花数是指一个三位十进制数 该数的各位数字立方之和等于该数本身 例如 153 是一个水仙花数 因为 1 3 5 3 3 3 153 实现代码 public class Test001 static int b bb bbb public static void main String args System out println 水仙花数有 int count 0 for int num 101 num 1 i a a 1 2 System out print 第一天共摘了 a 个桃子 打印结果为 第一天共摘了 1534 个桃子 20 一个两位数的正整数 如果将其个一个两位数的正整数 如果将其个 位数与十位数字对调所生成的数称为其位数与十位数字对调所生成的数称为其 对调数 如对调数 如 28 是是 82 的对调数 现给定的对调数 现给定 一个两位的正整数一个两位的正整数 46 请找到另一个 请找到另一个 两位的整数 使这两个数之和等于它们两位的整数 使这两个数之和等于它们 的各自的对调数之和 这样的另一个两的各自的对调数之和 这样的另一个两 位数有多少个 位数有多少个 实现代码 public class Test001 static int demo 46 public static void main String args int demoSum demo 10 demo 10 10 int count 0 System out println 这样的两位数有 for int i 10 i 100 i int shi i 10 int ge i 10 10 int sum shi ge if demo i demoSum sum count System out print i System out println r n 共有 count 个 21 求求 1 200 之间的能被之间的能被 7 整除的数整除的数 的平方和的平方和 实现代码 public class Test001 public static void main String args int sum 0 for int i 1 i 200 i if i 7 0 sum sum i i System out println 1 200 之间的能被 7 整除的数的平方和 sum 22 求求 1 99 的平方根的和并输出结果 的平方根的和并输出结果 保留小数点两位 保留小数点两位 实现代码 public class Test001 public static void main String args double sum 0 for int i 1 i 99 i sum sum Math sqrt i System out println 1 99 的平方根的和 sum 23 求求 351 432 之间既不能被之间既不能被 3 整除整除 也不能被也不能被 8 整除的正整数的个数整除的正整数的个数 实现代码 public class Test001 public static void main String args int count 0 System out println 351 432 之间既不能被 3 整除 也不能被 8 整除的正整数有 for int i 351 i 432 i if i 3 0 System out print i System out println r n 共有 count 个 24 已知已知 24 有有 8 个正整数因子个正整数因子 即 1 2 3 4 6 8 12 24 而 24 正好被其因子个数 8 整除 问 100 300 之间有多少个这样 的数 实现代码 import java util ArrayList import java util List public class Test001 public static void main String args int k int flag 0 int size 0 System out println 100 300 之间符合题意的正整数有 for int n 100 n 300 n List list new ArrayList for int i 1 i n i if n i 0 list add i size list size if n size 0 System out print n flag System out println r n 共有 flag 个 扩展 求任意一个正整数的正整数因子求任意一个正整数的正整数因子 要求 用户任意输入一个 int 型的正整数 然后程序打印出此正整数的所有因子 如 用户输入 24 则打印出 1 2 3 4 6 8 12 24 实现代码 import java util ArrayList import java util List import java util Scanner public class Test002 public static void main String args System out println 请输入一个正整数 Scanner sc new Scanner System in int num sc nextInt List list getNum num 将用户输入的正整数传到 getNum 方法中进行处理 返回一个 list 集合 此集合中就是符合题意的因子 System out println num 的正整数因子有 for Integer i list System out print i public static List getNum int demo List list new ArrayList int t 1 for int i 1 i demo i if demo i 0 list add i return list 25 多因子完备数 多因子完备数 若某整数 N 的所有因子之和等于 N 的倍数 则 N 称为多因子完备数 如数 28 其因子之和 1 2 4 7 14 28 56 2 28 28 是多因子完备数 求 1 200 之间有多少个多因子完备数 实现代码 public class Test001 public static void ma

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论