JAVA面试--一道上机题
来源:互联网 发布:2015年白酒行业数据 编辑:程序博客网 时间:2024/05/22 05:24
题目:给出一个整数数组,将其写入一个文件,再从文件中读出,并按整数大小逆序打印。
这道面试题是我看了好多面试题后自己编的,希望能对将要面试的人有点帮助。
对于我这个新手来说,觉得这个题目考察的基础东西还是比较多的:
1.对文件的写入和读出方面的考察
2.对不同形的数组转换方面的考察
3.对string基本用法的考察(split,substring)
4.类型转换的考察
5.对算法的考察(排序)
ps:我写的这个题目肯定有其他更好的方法,我这个就算是一个笨方法了,由于我编程功力还不深厚,而且写下面的这些代码借助了myEclipse这样先进的IDE工具,还时不时的上baidu ,google搜索一下,但在笔试的时候全屏记忆在纸上写,这样难度可想而知。看来基础真的很重要
- package com.xyq.demo;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- /**
- *
- * @author xyq
- *
- * 作用:一道面试题
- */
- public class T1 {
- /**
- * 把数组写入文件中
- * @throws IOException
- */
- void writefile() throws IOException {
- String str = "";
- FileOutputStream fos = new FileOutputStream("Result.txt");
- OutputStreamWriter osw = new OutputStreamWriter(fos);
- BufferedWriter bw = new BufferedWriter(osw);
- int[] intArr = new int[] { 10, -101,2, 5, 3, 4, 6 };
- //注意在这里输入到文件中的是字符串类型String而不是数组类型
- for (int i = 0; i < intArr.length; i++) {
- str += intArr[i] + ",";
- }
- str = str.substring(0, str.length()-1);
- bw.write(str);
- bw.close();
- }
- /**
- * 把数组从文件中读出来,并排序
- * @throws IOException
- */
- void readfile() throws IOException {
- String str = "";
- String str1 = "";
- FileInputStream fis = new FileInputStream("Result.txt");
- InputStreamReader isr = new InputStreamReader(fis);
- BufferedReader br = new BufferedReader(isr);
- //从文件中读出字符串String类型
- str = br.readLine();
- //把字符串转换成数组类型,再进行排序
- String[] strArr = str.split(",");
- //把String数组转换成int数组
- int[] intArr=new int[strArr.length];
- for(int i=0;i<strArr.length;i++){
- intArr[i]=Integer.parseInt(strArr[i]);
- }
- sort(intArr);
- for (int i = 0; i < intArr.length; i++) {
- str1 += intArr[i]+",";
- }
- str1 = str1.substring(0, str1.length()-1);
- System.out.print(str1);
- br.close();
- }
- /**
- * 冒泡排序算法
- * @param data
- */
- public static void sort(int[] data) {
- int temp;
- for (int i = 0; i < data.length; i++) {
- for (int j = data.length - 1; j > i; j--) {
- if (data[i] < data[j]) {
- temp = data[i];
- data[i] = data[j];
- data[j] = temp;
- }
- }
- }
- }
- /**
- * main方法
- * @param args
- * @throws IOException
- */
- public static void main(String[] args) throws IOException {
- new T1().writefile();
- new T1().readfile();
- }
- }
运行结果:
在Result.txt中为:10,-101,2,5,3,4,6
控制台打印为:10,6,5,4,3,2,-101
写完了,希望有更好方法或觉得我代码的不合理的朋友能指点一二,我最近要去面试,呵呵
- JAVA面试--一道上机题
- 一道java面试基础题
- 一道上机题
- 一道上机题
- 一道华为上机题
- 一道华为上机题
- 一道华为上机题
- 一道Java面试题目
- 面试的一道题
- 一道面试算法题
- 华为面试上机题及答案 JAVA写的
- 清华的一道复试上机题.......
- intel面试的一道题
- 一道经典面试小题
- Google面试一道算法题
- 一道概率题---网易面试
- Google面试一道排序题
- 微软面试 、华为上机题
- Android 几个Info系列类的总结
- Is this a good times or not
- 关于如何将char*转换成long型整数
- JDBC防止SQL注入(菜鸟笔记)
- AS3 中Label相关属性的设置
- JAVA面试--一道上机题
- VS2005 菜单项添加事件 VC2005消息映射
- VirtualBox设置Shared Folder的步骤
- 重载OnOK、OnCancle、OnClose
- SAP系统常用变量
- [SPC]生产统计过程控制三
- SQL 10 函数 3 日期时间函数 - 1 日期、时间、日期时间与时间戳
- 实现HTML页面在手机浏览器上全屏的方式
- 看看咱当兵的???看了顶一下!