1.判断一个数字是否为回文数
package com.gj.test;
/**
* @author Administrator 判断一个数字是否为回文数 <br>
* 解题思路:将数字倒序排序后,比较是否相等
*
*/
public class HuiWenShuTest {
public static void main(String[] args) {
int a = 12331;
boolean flag = isHuiWenShu(a);
if (flag) {
System.out.println("数字a是回文数");
} else {
System.out.println("数字a不是回文数");
}
}
public static boolean isHuiWenShu(int num) {
int result = reverseNum(num);
if (result == num) {
return true;
}
return false;
}
public static int reverseNum(int num) {
int revNum = 0;
while (num != 0) {
// 取余数
revNum = num % 10 + revNum * 10;
System.out.println("revNum:" + revNum);
num /= 10;
System.out.println("num:" + num);
}
return revNum;
}
}
2.遍历一个路径目录下的所有文件
package com.gj.test;
import java.io.File;
import java.io.IOException;
/**
* @author Administrator 遍历路径:F:\前端表格控件 目录下的所有文件
*
*/
public class FileTest {
public static void main(String[] args) throws IOException {
String path = "F:\\前端表格控件";
findFile(path);
}
public static void findFile(String path) throws IOException {
File fl = new File(path);
// 判断文件目录是否存在
if (!fl.exists()) {
System.out.println("file path is not exit");
return;
}
// 判断文件是否是一个目录
if (!fl.isDirectory()) {
if (fl.isFile()) {
System.out.println(fl.getCanonicalFile());
}
return;
}
String[] list = fl.list();
for (String temp : list) {
File dirFile = new File(path, temp);
if (!dirFile.isDirectory()) {
if (dirFile.isFile()) {
System.out.println(dirFile.getCanonicalFile());
}
continue;
} else {
findFile(dirFile.getAbsolutePath());
}
}
}
}
3.获取一年后的今天日期
package com.gj.test;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;
/**
* @author Administrator
*
* 获取一年后的今天日期
*
*/
public class DateTest {
public static void main(String[] args) {
LocalDate today = LocalDate.now();
System.out.println("今天的日期:" + today);
int oneYear = 1;
LocalDate oneYears = today.plus(oneYear, ChronoUnit.YEARS);
System.out.println("一年后的今天的日期:" + oneYears);
Calendar calendar = Calendar.getInstance();
Date cadate = calendar.getTime();
System.out.println("cadate=" + cadate);
Date date = new Date(System.currentTimeMillis());
System.out.println("当前日期:" + date);
calendar.setTime(date);
calendar.add(Calendar.YEAR, 1);
date = calendar.getTime();
System.out.println("一年后的今天日期:" + date);
}
}
4.写出输出结果
package com.gj.vo;
public class Parent {
public Parent() {
System.out.println("Parent contructor");
}
{
System.out.println("this is parent");
}
static {
System.out.println("hello,parent static");
}
public void print() {
System.out.println("this is Parent print");
}
}
package com.gj.vo;
public class Son extends Parent {
public Son() {
System.out.println("Son contructor");
}
{
System.out.println("this is son");
}
static {
System.out.println("hello son static");
}
public void print() {
System.out.println("this is son print");
}
}
package com.gj.test;
import com.gj.vo.Parent;
import com.gj.vo.Son;
/**
* @author Administrator 子类与父类相关问题
*
*/
public class FuZiLeiTest {
public static void main(String[] args) {
// 问题1,输出的结果是:先是执行父类的静态static块,子类的静态static块,然后是执行父类的{}语句块和构造方法,最后是执行子类的{}语句块和构造方法
Parent p = new Son();
// 输出结果:
/**
* hello,parent static <br>
* hello son static<br>
* this is parent <br>
* Parent contructor<br>
* this is son <br>
* Son contructor <br>
*
*/
p.print();// 输出:this is son print
}
}
5.Integer类的==
package com.gj.test;
import com.gj.vo.Person;
public class IntegerTest {
/**
* @param args
* Integer类在内存中有一个值的范围为[-128,127]的对象池。只要Integer对象的值在[-128,127]范围内
* ,都是从这个对象池中取。所以只要是这个范围内的Integer对象,只要值相同,就是同一个对象。那么==的结果,就是true。
* 超过了这个范围,则会new新的Integer对象,尽管值相同,但是已经是不同的对象了
*
*/
public static void main(String[] args) {
Integer a = 100, b = 100, c = 150, d = 150;
System.out.println("Integer的上限:" + Integer.MAX_VALUE);
System.out.println("c.hashcode==" + c.hashCode());
System.out.println("d.hashcode==" + d.hashCode());
System.out.println(a == b);// true
System.out.println(c == d);// false
System.out.println(c.equals(d));// true
Float s = 12.2f;
Float w = 12.2f;
System.out.println(s == w);// false
System.out.println(s.equals(w));// true
Person p1 = new Person();
Person p2 = new Person();
System.out.println(p1 == p2);// false
System.out.println(p1.equals(p2));// false
}
}
6.冒泡排序算法
package com.gj.test;
/**
* @author Administrator
*
* 冒泡排序算法
*
*/
public class MaoPaoSortTest {
public static void main(String[] args) {
int[] arr = { 6, 1, 2, 5, 7, 8, 4 };
sort2(arr);
for (int tep : arr) {
System.out.println(tep);
}
}
// 实现1
public static void sort1(int[] arr) {
int temp;
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
// 实现2
public static void sort2(int[] arr) {
int temp;
for (int i = 0; i < arr.length; i++) {
for (int j = arr.length - 1; j > i; j--) {
if (arr[j] < arr[j - 1]) {
temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
}
}
}
}
}
7、 遍历map
package com.gj.test;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
/**
* 遍历map<br>
*
* @author Administrator
*
*/
public class MapTest {
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
// 方法一
for (Entry<String, String> entry : map.entrySet()) {
System.out.println("key:" + entry.getKey());
System.out.println("value:" + entry.getValue());
}
}
}
8 . 使用正则表达式匹配一个带两位小数的正数
package com.gj.test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author Administrator 使用正则表达式匹配一个带两位小数的正数
*
*/
public class PatternTest {
public static void main(String[] args) {
String a = "233.224";
// 第一种
String regexs = "^\\d+(\\.\\d(2))$";
// 第二种
String regex = "^[1-9][0-9]*+(\\.[0-9]{2})$";
Pattern p = Pattern.compile(regex);
Matcher matcher = p.matcher(a);
if (matcher.matches()) {
System.out.println("是带两位小数的正数");
} else {
System.out.println("不是带两位小数的正数");
}
}
}
9、100除以3,赋值给double变量C,保留两位小数
package com.gj.test;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
/**
* @author Administrator 问题: 100除以3,赋值给变量C,保留两位小数
*/
public class RemainTest {
public static void main(String[] args) {
// 方法一
BigDecimal a = new BigDecimal(100);
BigDecimal b = new BigDecimal(3);
BigDecimal real = a.divide(b, 2, 4);// 此方法快被舍弃,优先使用下面方法;
// 第二个参数代表保留几位,第三个参数代表舍入模式
BigDecimal other = a.divide(b, 2, RoundingMode.DOWN);
System.out.println("real = " + real);// 33.33
System.out.println("other = " + other);// 33.33
BigDecimal d = new BigDecimal(14.3628);
double c = d.setScale(2, BigDecimal.ROUND_DOWN).doubleValue();// 对已知数据进行舍入
System.out.println("c==" + c);// 14.36
// ******************/
// 方法二
DecimalFormat df = new DecimalFormat("0.00");
int sx = 100;
int cs = 3;
String e = df.format((double) sx / cs);
double f = Double.valueOf(e);
System.out.println("e=" + e);
System.out.println("f=" + f);
}
}
10、 单例模式实现
package com.gj.test;
/**
* @author Administrator
*
* 单例模式
*
*/
public class Singleton {
private static Singleton instance;
private Singleton() {
}
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
return instance;
}
return instance;
}
}
11、 String类相关问题
package com.gj.test;
/**
* @author Administrator 比较字符串+整型运算结果;
*/
public class StringTest {
public static void main(String[] args) {
// *****比较字符串+整型运算结果***/
int a = 3;
int b = 4;
String c = "";
c = a + b + c;// 先是两个整型相加,然后转为String与空字符串拼接
System.out.println("c = a + b + c:" + c);// 7
c = a + c + b;// a先转成String类型,然后与字符串C进行拼接,然后再将b转为String,与之前结果拼接
System.out.println("c=a+c+b:" + c);// 374
c += a;
System.out.println("c+=a:" + c);// 3743
c = c + a + b;
System.out.println("c=c+a+b:" + c);
// *****比较字符串是否相等***/
String str1 = "hello";
String str2 = "he" + new String("llo");
String str3 = "he" + "llo";
System.out.println(str1 == str2);// false
System.out.println(str1 == str3);// true
System.out.println(str1.equals(str2));// true
System.out.println(str1.equals(str3));// true
String name = "hello world";
reseverString2(name);
}
/**
* 将字符串倒序<br>
*
* @param param
* @return
*
*/
// 方法一
public static String reseverString1(String param) {
System.out.println("开始参数,param:" + param);
char[] arr = param.toCharArray();
String result = "";
for (int i = arr.length - 1; i >= 0; i--) {
result += arr[i];
}
System.out.println("结束参数,result:" + result);
return result;
}
// 方法二
public static String reseverString2(String param) {
System.out.println("开始参数,param:" + param);
StringBuffer sb = new StringBuffer(param);
StringBuffer result = sb.reverse();
System.out.println("结束参数,result:" + result.toString());
return result.toString();
}
}
12 、对list集合,按照集合里对象的name排序
package com.gj.vo;
import java.io.Serializable;
public class Person implements Serializable {
private static final long serialVersionUID = 7846874432622658899L;
private String name;
private String sale;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSale() {
return sale;
}
public void setSale(String sale) {
this.sale = sale;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package com.gj.test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import com.gj.vo.Person;
/**
* @author Administrator
*
* 对list集合,按照集合里对象的name排序
*
*/
public class SortTest {
public static void main(String[] args) {
List<Person> list = new ArrayList<Person>();
Person ps = null;
for (int i = 1; i < 5; i++) {
ps = new Person();
ps.setAge(i);
ps.setName("Test" + i);
list.add(ps);
}
// 倒序
Collections.sort(list, new Comparator<Person>() {
@Override
public int compare(Person o1, Person o2) {
if (o1.getName().compareTo(o2.getName()) > 0) {
return -1;
}
if (o1.getName().compareTo(o2.getName()) < 0) {
return 1;
}
return 0;
}
});
for (Person p1 : list) {
System.out.println(p1.getName());
}
}
}
13、switch表达式后面的数据类型只能是byte,short,char,int四种整形类型,枚举类型和java.lang.String类型(从java 7才允许)
14、异常处理中,如果一个方法里的try代码块一定抛出异常并被catch捕获,但是catch也抛出异常,finally块直接return,那么调用这个方法的方法是否能捕获到异常?
答案是不能捕获,因为不管try或catch里面是什么代码,最后都会执行finally里面的代码
15、父子类继承问题:
package com.gj.vo;
import java.util.Map;
public class A {
private Map m;
public Map getM() {
return m;
}
public void setM(Map m) {
this.m = m;
}
public Map aa() {
return this.m;
}
}
package com.gj.vo;
import java.util.Map;
public class B extends A {
public Map bb() {
return aa();
}
}
package com.gj.vo;
public class C extends B {
}
package com.gj.test;
import com.gj.vo.B;
import com.gj.vo.C;
public class ABCTest {
public static void main(String[] args) {
C c = new C();
B b = new B();
System.out.println(c.aa() == b.aa());// true
System.out.println(c.bb() == b.aa());// true
System.out.println(c.aa() == b.getM());// true
System.out.println(c.getM() == b.aa());// true
}
}