后端面试-笔试题
1、字符串:String host=”http://www.idawa.cn/”;请解析出www.idawa.cn 并统计a 出现的次数
public static void main(String[] args) { String host="http://www.idawa.cn/"; int start = host.lastIndexOf("//")+2; int end = host.lastIndexOf("/"); String str = host.substring(start,end); System.out.println(str); byte[] bytes = host.getBytes(); int num=0; char a='a'; for (byte b : bytes) { if (a==b){ num++; } } System.out.println(num); }
2、数组[9,4,7,8,3,4,6] 先定义数组先去重复,最后降序排列
public static void main(String[] args) { Integer[] intArr={9,4,7,8,4,6}; List<Integer> intOldList = Arrays.asList(intArr); List<Integer> intNewList = new ArrayList<>(); //使用set 进行去重处理 Set<Integer> intSet = new HashSet<>(intOldList); for (Integer integer : intSet) { intNewList.add(integer); } Integer [] toArray = new Integer[intNewList.size()]; for (int i = 0; i < intNewList.size(); i++) { toArray[i] = intNewList.get(i); } Integer[] sort = sort(toArray); for (Integer integer : sort) { System.out.println(integer); } } //进行冒泡排序 public static Integer[] sort(Integer[] s){ int length = s.length; for (int i = 0; i < length; i++) { for (int j = 0; j < length-i-1; j++) { if (s[j+1] < s[j]){ Integer b=s[j]; s[j]=s[j+1]; s[j+1]=b; } } } return s; }
3、定义一个Hashmap<String,String> 放入3个个元素[(“1”,“2”),(“2”,“2”),(“3”,“3”)] 根据value值去重后遍历打印
public static void main(String[] args) { Map<String,String> map=new HashMap(); map.put("1","2"); map.put("2","2"); map.put("3","3"); quchong(map); } public static void quchong(Map<String, String> map){ Map<String,String> endMap=new HashMap(); Set<String> temp = new HashSet<>(); Set<String> stringSet = map.keySet(); for (String key : stringSet) { //根据set是否添加进去值 判断是否为重复值 set添加时如果遇到重复值则返回false boolean add = temp.add(map.get(key)); if (add){ endMap.put(key,map.get(key)); } } System.out.println(endMap.size()); Set<Map.Entry<String, String>> entries = endMap.entrySet(); for (Map.Entry<String, String> entry : entries) { System.out.println("key:"+entry.getKey()+";value:"+entry.getValue()); } }
4、递归实现:列出D盘所有的文件名后缀为doc的文件
static List<String> result=new ArrayList<>(); public static void main(String[] args) { File file=new File("D://"); searchFile(file); for (String fileName : result) { System.out.println(fileName); } } public static void searchFile(File file){ File[] files = file.listFiles(); if (files != null && files.length > 0){ for (File file1 : files) { if (file1.isDirectory()){ searchFile(file1); }else if (file1.isFile() && file1.getName().endsWith("doc")){ result.add(file1.getName()); } } } }
5、sql笔试题
EXPLAIN select st.id,sum(score) s from student st INNER JOIN score so on st.id=so.id where sex="女" GROUP BY st.id HAVING s>350 order by s asc LIMIT 20,10