Java 基础题

 在网上找了点基础题目,自己做了做,下面是题目:


  1.Java有那些基本数据类型,String是不是基本数据类型,他们有何区别。
  2.字符串的操作:
   写一个方法,实现字符串的反转,如:输入abc,输出cba
   写一个方法,实现字符串的替换,如:输入bbbwlirbbb,输出bbbhhtccc。
  3.数据类型之间的转换
   如何将数值型字符转换为数字(Integer,Double)
   如何将数字转换为字符
   如何取小数点前两位,并四舍五入。
  4.日期和时间
   如何取得年月日,小时分秒
   如何取得从1970年到现在的毫秒数
   如何获取某个日期是当月的最后一天
   如何格式化日期
  5.数组和集合
  6.文件和目录(I/O)操作
   如何列出某个目录下的所有文件
   如何列出某个目录下的所有子目录
   判断一个文件或目录是否存在
   如何读写文件
  7.Java多态的实现(继承、重载、覆盖)
  8.编码转换,怎样实现将GB2312编码的字符串转换为ISO-8859-1编码的字符串。
  9.Java中访问数据库的步骤,Statement和PreparedStatement之间的区别

 

2.

private String getString(String str) { String reString=""; for (int i = str.length()-1; i >=0 ; i--) { reString +=str.charAt(i); } return reString; }

 

实现字符串的替换,如:输入bbbwlirbbb,输出bbbhhtccc 不明白题目是什么意思

 

 

3private void GetReplase() { //方法1 先乘以100然后四舍五入,然后再除以100 double a=3.1675; int b=(int)Math.round(a*100); double c=(double)b/100.00; System.out.println(c); //使用格式化 DecimalFormat dFormat=new DecimalFormat("#.00"); System.out.println(dFormat.format(a)); }

 

 

4

static void GetDate(){ Date date=new Date(); System.out.println(date); SimpleDateFormat sFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(sFormat.format(date)); Calendar calendar=Calendar.getInstance(); calendar.setTime(new Date()); System.out.println(calendar.get(Calendar.YEAR)); System.out.println(calendar.get(Calendar.MONTH)+1); System.out.println(calendar.get(Calendar.DAY_OF_MONTH)); System.out.println(calendar.get(Calendar.HOUR_OF_DAY)); System.out.println(calendar.get(Calendar.MINUTE)); System.out.println(calendar.get(Calendar.SECOND)); }

 

 

5.取得从1970年到现在的毫秒数

 

public static Long GetMilliseconds() { Calendar calendar=Calendar.getInstance(); Date date=new Date(); Long mLong1=date.getTime(); calendar.set(1970, 0, 1); Long mLong2=calendar.getTimeInMillis(); return mLong1-mLong2; }

 

6.如何获取某个日期是当月的最后一天

public static boolean GetLastDay(Date date) { int oldMonth,newMonth=0; Calendar calendar=Calendar.getInstance(); calendar.setTime(date); oldMonth=calendar.get(Calendar.MONTH); System.out.println(oldMonth); calendar.add(Calendar.DAY_OF_MONTH, 1); newMonth=calendar.get(Calendar.MONTH); System.out.println(newMonth); if (oldMonth!=newMonth) { return true; } else { return false; } }

 

7.如何格式化日期

public static void SetFormat() { Date date=new Date(); SimpleDateFormat sFormat=new SimpleDateFormat("yyyy-MM-dd HH:MM:ss"); System.out.println(sFormat.format(date)); }

 

 

8.如何列出某个目录下的所有文件

  public static void Getfiles() { File file=new File("G://工程"); for (File f : file.listFiles()) { if (f.isFile()) { System.out.println(f.getName()); } } }

 

9.如何列出某个目录下的所有子目录
public static void GetSonDic() { File file=new File("G://工程"); for (File string : file.listFiles()) { if (string.isDirectory()) { System.out.println(string.getName()); } } }

 

10.判断一个文件或目录是否存在

public static void IsNew() { File file=new File("G://工程"); if (file.exists()) { System.out.println("文件存在!"); } else { System.out.println("文件不存在!"); } }

 

你可能感兴趣的:(java,Date,String,calendar,File,Integer)