Java 笔试基础(一)

1.第一题(观察打印效果)
public class StringDemo {
    public static void main(String[] args) {
        //s1和s2虽然内容相同,但是确实创建了两个不同的堆内存空间,也有两个栈内存
        String s1 = new String("java");
        StringBuffer s2 = new StringBuffer("java");
        String text1 = function1(s1);
        StringBuffer text2 = function2(s2);
        System.out.println(text1 + text2);
    }
    public static String function1(String text) {
        return text.replaceAll("j","1");
    }
    /**
     *  StringBuffer append(char c) 
     *  将 char 参数的字符串表示形式追加到此序列
     */
    public static StringBuffer function2(StringBuffer text) {
        return text.append("c");
    }
}
打印结果:
1avajavac
2.第二题
public class NumberDemo {
    public static void main(String[] args) {
        /**
         * 2.任取一个四位数字组成的四位正整数,
         * 把其每位上的数字排成最大值和最小值(也是四位数)相减刚好还是这个数,
         * 求这个四位正整数
         */
         //1.首先开始循环找寻这个四位正整数
         //2.拿到这个数,先分成个,十,百,千四位数字
         //3.先进行从小到大排序(冒泡)
         //4.进行组装最大值和最小值
         //5.验证,最小值首数字应不为0
         //6.符合要求即输出

         for(int number=1000;number<10000;number++) {
            int a = number%10;  //拿到个位
            int b = (number%100-a)/10;  //拿到十位
            int d = number/1000;    //拿到千位
            int c = number/100-d*10;//拿到百位

            int[] arraySort = sortNumber(a,b,c,d);  //冒泡排序

            int minNumber = arraySort[3]*1 + arraySort[2]*10 + arraySort[1]*100 + arraySort[0]*1000;
            int maxNumber = arraySort[0]*1 + arraySort[1]*10 + arraySort[2]*100 + arraySort[3]*1000;
            if((minNumber/1000)!=0&&((maxNumber-minNumber)==number)) {  //进行验证
                System.out.println("满足条件的四位数为:" + number);
            }
         }
    }
    public static int[] sortNumber(int a,int b,int c,int d) {
        int[] arrayNumber = {a,b,c,d};
        int temp;
        for(int j=1;j<arrayNumber.length;j++) {
            for(int i=0;i<arrayNumber.length;i++) {
                if(arrayNumber[i]>arrayNumber[j]) {
                    temp = arrayNumber[i];
                    arrayNumber[i] = arrayNumber[j];
                    arrayNumber[j] = temp;
                }
            }
        }
        return arrayNumber;
    }
}
打印结果:
满足条件的四位数为:6174
3.第三题
package BydDemo;

/**
 * 3.利用Java写一个方法用于保存Txt文件
 *
 */
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class BydDemo01 {
    public static void main(String[] args) {
        String fileName = "/1.txt";
        String content = "利用Java写一个方法用于保存Txt文件";
        if(saveFile(fileName,content)) {
            System.out.println("写入保存成功!");
        } else{
            System.out.println("写入保存失败!");
        }
    }
    public static boolean saveFile(String fileName,String content) {
        String path = "E:/testFolder";
        boolean success = true;
        File file = new File(path + fileName);  //文件路径
        FileOutputStream fos = null;
        if(!file.getParentFile().exists()) {    //此文件的父文件夹是否存在
            file.getParentFile().mkdirs();  //创建父文件夹
        }   
        try{
            file.createNewFile();   //创建新文件
            fos = new FileOutputStream(file);
            byte[] b = content.getBytes();  //把字符串转换为字节数组
            fos.write(b);   //进行写操作
        } catch(Exception e) {
            success = false;
            e.printStackTrace();
        } finally {
            if(fos!=null) { 
                try {
                    fos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }   
        }
        return success;
    }
}
打印结果:
写入保存成功!
4.第四题
package BydDemo;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class BydDemo02 {
    /*
     * 4.编写一个读取某个Txt文件的内容,并把它输出到控制台, 
     * 假如某个a.txt文件,里面有三行内容,打印到控制台也有三行 
     * 111111 
     * 2222222
     * 33333
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        printFile("/a.txt");
    }

    public static void printFile(String fileName) {
        String path = "E:/testFolder";
        String file = path + fileName;
        FileReader fr = null;
        BufferedReader buff = null;
        try {
            fr = new FileReader(file);
            buff = new BufferedReader(fr);
            String s = null;
            while((s = buff.readLine())!=null) {
                System.out.println(s);
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally{
            if(fr!=null) {
                try {
                    fr.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(buff!=null) {
                try {
                    buff.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }           
            }
        }
    }
}
打印结果:
111111 
2222222
33333
5.第五题
package BydDemo;

public class BydDemo06 {
    /**
     * 5.传入一个数字数组,返回数字之和,如果数字是5则跳过不累加,
     * 如果数字大于8就停止累加,返回结果;传入[1,3,5,9,1] = 4;
     * [1,3,5,2,9,1] = 6;
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[] array1 = {1,3,5,9,1};     //声明两个数组
        int[] array2 = {1,3,5,2,9,1};
        System.out.println("第一个数组之和:" + addAll(array1));
        System.out.println("第二个数组之和:" + addAll(array2));
    }
    public static int addAll(int[] arrayNumber) {
        int sum = 0;
        for(int i=0;i<arrayNumber.length;i++) {     //遍历数组
            if(arrayNumber[i]==5) {
                continue;   //跳过此次循环,不累加
            }
            if(arrayNumber[i]>8) {
                break;      //终止循环体         
            }
            sum += arrayNumber[i];
        }
        return sum;

    }

}
打印结果:
第一个数组之和:4
第二个数组之和:6
6.第六题
package BydDemo;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

public class BydDemo07 {
    /**
     * 6.传入一个List,返回List中是数字的集合:
     * List中包含"a23","1","2","c";    
     * 返回"1","2"
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //1.ArrayList用数组存储元素,且数组是动态创建
        //2.若元素个数超过数组容量,就创建一个更大的新数组,并将当前数组中的所有元素复制到新数组中
        //3.可通过下标随机访问元素。除在末尾处之外,不能在其他位置插入或删除元素
        List<String> arrayList1 = new ArrayList<String>();  //创建数组链表
        arrayList1.add("a23");  //添加元素
        arrayList1.add("1");
        arrayList1.add("2");
        arrayList1.add("c");
        List<String> arrayList2 = getNumberList(arrayList1);    //筛选出数字,并放入ArrayList集合返回
        printArrayList(arrayList2);     //打印ArrayList集合中的元素
    }

    public static List<String> getNumberList(List<String> list) {   //筛选
        // TODO Auto-generated method stub
        List<String> arrayList2 = new ArrayList<String>();  //存放满足条件的元素
        for(int i=0;i<list.size();i++) {
             //E get(int index) 
              //返回此列表中指定位置上的元素
            if(isNumeric(list.get(i))) {    //判断是否为数字
                arrayList2.add(list.get(i));
            }
        }
        return arrayList2;

    }
    public static void printArrayList(List<String> list) {  //打印元素
        // TODO Auto-generated method stub
        ListIterator< String> listIterator = list.listIterator();   //实例化ListIterator
        System.out.println("打印集合中的数字:");
        while(listIterator.hasNext()) {     //若有下一个元素
            System.out.print(listIterator.next() + " ");    //返回下一个元素
        }
    }
    public static boolean isNumeric(String str) {   //判断输入的字符串为数字
        // TODO Auto-generated method stub
        for(int i=0;i<str.length();i++) {   //遍历字符串
            char ch = str.charAt(i);    //取出单个字符
            if(!Character.isDigit(ch)) {    //判断单个字符属于数字,只要一个字符不符合就是false
                return false;
            }
        }
        return true;    //整个字符串都满足返回true
    }
}
打印结果:
打印集合中的数字:
1 2 
7.第七题
package BydDemo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Set;

public class BydDemo08 {
    /**
     * 7.传入一个Map,返回Map中值是数字的key集合,传入
     * [a:1],[b:"ad"],[c:"3"]返回“a”,“c”
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Map<String, String> hashMap1 = new HashMap<String,String>();    //创建HashMap
        hashMap1.put("a", "1"); //添加元素
        hashMap1.put("b", "ad");
        hashMap1.put("c", "3");
        List<String> arrayList = getNumberMap(hashMap1);
        printArrayList(arrayList);      //打印ArrayList集合中的元素
    }

    public static void printArrayList(List<String> arrayList) { //打印输出
        // TODO Auto-generated method stub
        ListIterator<String> listIterator = arrayList.listIterator();   //实例化ListIterator
        System.out.println("输出符合的结果:");
        while(listIterator.hasNext()) {
            System.out.print(listIterator.next() + " ");
        }
    }

    public static List<String> getNumberMap(Map<String, String> hashMap) {  //判断Map中值是数字,然后把key传入到ArrayList中
        // TODO Auto-generated method stub
        List<String> arrayList = new ArrayList<String>();   //先创建要盛放的ArrayList
        Set<String> set = hashMap.keySet();     //拿到Map的键值对
        Iterator<String> iterator = set.iterator();
        while(iterator.hasNext()) {
            String key = iterator.next();
            String value = hashMap.get(key);    //使用Iterator遍历,有键位拿到值
            if(isNumeric(value)) {  //进行判断是否为数字
                arrayList.add(key);
            }
        }
        return arrayList;
    }

    public static boolean isNumeric(String str) {   //判断字符串为数字
        // TODO Auto-generated method stub
        for(int i=0;i<str.length();i++) {       //遍历字符串
            char ch = str.charAt(i);    //遍历每个字符
            if(!Character.isDigit(ch)) {    //只要有一个不是数字则返回false
                return false;
            }
        }
        return true;
    }
}
打印结果:
输出符合的结果:
a c 
8.第八题
package BydDemo;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class BydDemo09 {
    /**
     * 8.传入一个日期格式,返回当前的日期;传入"yyyy-MM-dd"
     * 返回"2014-09-05",传入"yy-MM-dd hh:mm:ss",
     * 返回"14-09-05 09:30:19"等
     * 
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //1.先输入日期格式:yyyy-MM-dd 和 yy-MM-dd hh:mm:ss
        System.out.println("请输出日期格式:");
        Scanner sc = new Scanner(System.in);
        String dataFormat = sc.nextLine();
        //2.传入方法返回输出
        //根据用户传入的时间表示格式,打印当前时间的格式
        getNowDataTime(dataFormat);
        if(sc!=null) {
            sc.close();
        }
    }

    public static void getNowDataTime(String dataFormat) {
        // TODO Auto-generated method stub
        Date currentTime = new Date();      //拿到当前准确的时间
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dataFormat);   //传入需要打印的格式
        String dataString = simpleDateFormat.format(currentTime);   //使用格式把本地时间转化
        System.out.println("日期格式为:" + dataFormat + ";打印效果为:" + dataString); //打印输出
    }
}
打印结果:
请输出日期格式:
yyyy-MM-dd
日期格式为:yyyy-MM-dd;打印效果为:2017-03-02

请输出日期格式:
yy-MM-dd hh:mm:ss
日期格式为:yy-MM-dd hh:mm:ss;打印效果为:17-03-02 06:09:58
9.第九题
package BydDemo;

import java.util.HashMap;
import java.util.Map;
public class BydDemo10 {
    /**
     * 9.写一个递归方法,传入一个Map,返回这个Map对象中一共
     * 有多少个Map类型的对象
     *      Map中是可以添加Map,被添加的这个Map也可以添加Map,
     * 所以需要判断传入的Map对象的所有子节点。如果是Map就 +1,
     * 同时对这个子Map也需要循环它的子节点,如果它子节点是Map,
     * 也需要+1;同时还需要继续循环这个子Map;
     */

    /**
     * 实现思路:遍历Map中的键值对,若键值对是Map类型,sum开始累加且递归。
     * (递归边界条件: 键值是否有map),有map就继续调用递归,没有就处理下一个键值,直到没有为止
     */
    public static int sum = -1; //设置此值,避免第一次传入Map对象而累加
    public static void main(String[] args) {

        Map<Object,Object> hashMap3 = new HashMap<Object,Object>(); //1.设置Map循环对象
        hashMap3.put("10", "i");
        hashMap3.put("11", "j");
        hashMap3.put("12", "k");

        Map<Object,Object> hashMap2 = new HashMap<Object,Object>();
        hashMap2.put("7", "f");
        hashMap2.put("8", hashMap3);
        hashMap2.put("9", "h");

        Map<Object,Object> hashMap1 = new HashMap<Object,Object>();
        hashMap1.put("4", "c");
        hashMap1.put(hashMap2, "d");
        hashMap1.put("6", hashMap2);

        Map<Object,Object> hashMap = new HashMap<Object,Object>();
        hashMap.put(hashMap1, "a");
        hashMap.put(2, "b");
        hashMap.put("3", hashMap1);
        int number = isGoneCycle(hashMap);  //2.传入Map对象,进行循环遍历
        System.out.println("总共的Map对象:" + number);
    }

    public static int isGoneCycle(Object hashMap1) {
        if(hashMap1 instanceof Map) {       //递归边界条件: 键值是否有map
            sum ++;
            //遍历map中的键
            Map<Object,Object> hashMap = (Map<Object, Object>) hashMap1;
            for (Object key : hashMap.keySet()) {   //对键位进行遍历
                System.out.println("Key = " + key);
                isGoneCycle(key);
            }
            System.out.println("****分割线****");      
            //遍历map中的值
            for (Object value : hashMap.values()) { //对值进行遍历
                System.out.println("Value = " + value);
                isGoneCycle(value);
            }
            return sum;     //返回总共的对象
        } else{
            return 0;
        }
    }   
}
打印结果:
Key = 2
Key = 3
Key = {4=c, {7=f, 8={11=j, 12=k, 10=i}, 9=h}=d, 6={7=f, 8={11=j, 12=k, 10=i}, 9=h}}
Key = 4
Key = {7=f, 8={11=j, 12=k, 10=i}, 9=h}
Key = 7
Key = 8
Key = 9
****分割线****
Value = f
Value = {11=j, 12=k, 10=i}
Key = 11
Key = 12
Key = 10
****分割线****
Value = j
Value = k
Value = i
Value = h
Key = 6
****分割线****
Value = c
Value = d
Value = {7=f, 8={11=j, 12=k, 10=i}, 9=h}
Key = 7
Key = 8
Key = 9
****分割线****
Value = f
Value = {11=j, 12=k, 10=i}
Key = 11
Key = 12
Key = 10
****分割线****
Value = j
Value = k
Value = i
Value = h
****分割线****
Value = b
Value = {4=c, {7=f, 8={11=j, 12=k, 10=i}, 9=h}=d, 6={7=f, 8={11=j, 12=k, 10=i}, 9=h}}
Key = 4
Key = {7=f, 8={11=j, 12=k, 10=i}, 9=h}
Key = 7
Key = 8
Key = 9
****分割线****
Value = f
Value = {11=j, 12=k, 10=i}
Key = 11
Key = 12
Key = 10
****分割线****
Value = j
Value = k
Value = i
Value = h
Key = 6
****分割线****
Value = c
Value = d
Value = {7=f, 8={11=j, 12=k, 10=i}, 9=h}
Key = 7
Key = 8
Key = 9
****分割线****
Value = f
Value = {11=j, 12=k, 10=i}
Key = 11
Key = 12
Key = 10
****分割线****
Value = j
Value = k
Value = i
Value = h
Value = a
总共的Map对象:10
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值