JavaSE编码试题强化练习6
1.写出选择排序的代码实现,对一个int数组进行排序
public class TestSelectSort {
public static void main(String[] args) {
int [] arr = {87,65,5,5,43,21};
System.out.print("排序前:[ ");
for (int i : arr){
System.out.print(i+" ");
}
System.out.println("]");
selectSort(arr);
System.out.print("排序后:[ ");
for (int i : arr){
System.out.print(i+" ");
}
System.out.println("]");
}
public static void selectSort(int[] arr){
for (int i = 0;i < arr.length - 1;i++){
int minIndex = i;
for (int j = i;j < arr.length - 1;j++){
if (arr[minIndex] > arr[j + 1]){
minIndex = j + 1;
}
}
if (minIndex != i){
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
}
}
运行结果:

2.使用IO包中的类读取E盘上exam.txt文本文件的内容,每次读取一行内容,将每行作为一个输入放入ArrayList的泛型集合中并将集合中的内容使用加强for进行输出显示。
public class TestReader {
public static void main(String[] args) {
String path = "E:/exam.txt";
outputMethod(path);
}
private static void outputMethod(String path) {
/**
* 创建集合对象
*/
List<String> list = new ArrayList<String>();
/**
* 创建缓冲区对象
*/
BufferedReader br= null;
try {
br = new BufferedReader(new FileReader(path));
/**
* 读取数据每次读取一行
*/
String line = br.readLine();
while (line != null){
list.add(line);
line = br.readLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if (br != null){
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
for (String s : list){
System.out.println(s);
}
}
}
}
文本内容:

程序运行结果:

注意:文本内容要以UTF-8格式保存,不然程序读取时会报乱码,如下图(文本以ANSI格式保存的):

3.编写两个线程,一个线程打印1-52的整数,另一个线程打印字母A-Z。打印顺序为12A34B56C….5152Z。即按照整数和字母的顺序从小到大打印,并且每打印两个整数后,打印一个字母,交替循环打印,直到打印到整数52和字母Z结束。
/**
* 打印类
*/
public class Printer {
/**
* 设为1,方便计算3的倍数
*/
private int index = 1; /**
* 打印数字的方法,每打印两个数字,等待打印一个字母
*/
public synchronized void print(int i){
while (index % 3 == 0){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.print(""+i);
index++;
notifyAll();
}
/**
* 打印字母,每打印一个字母等待打印两个数字
*/
public synchronized void print(char c){
while (index % 3 != 0){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.print(""+c);
index++;
notifyAll();
}
}
/**
* 打印数字线程
*/
public class NumberPrinter extends Thread {
private Printer p; public NumberPrinter(Printer p) {
this.p = p;
} public void run() {
for (int i =1;i <= 52;i++){
p.print(i);
}
}
}
/**
* 打印字母线程
*/
public class LetterPrinter extends Thread {
private Printer p;
public LetterPrinter(Printer p){
this.p = p;
}
public void run(){
for (char c = 'A';c <= 'Z';c++){
p.print(c);
}
}
}
public class TestThread {
public static void main(String[] args) {
/**
* 创建打印机对象
*/
Printer p = new Printer();
/**
* 创建线程对象并启动线程
*/
Thread t1 = new NumberPrinter(p);
Thread t2 = new LetterPrinter(p);
t1.start();
t2.start();
}
}
运行结果:

JavaSE编码试题强化练习6的更多相关文章
- JavaSE编码试题强化练习1
1. 编写应用程序,创建类的对象,分别设置圆的半径.圆柱体的高,计算并分别显示圆半径.圆面积.圆周长,圆柱体的体积. /** * 定义父类--圆类 */ public class Circle { / ...
- JavaSE编码试题强化练习7
1.编写应用程序,创建类的对象,分别设置圆的半径.圆柱体的高,计算并分别显示圆半径.圆面积.圆周长,圆柱体的体积. /** * 圆类 */ public class Circle { /** * 类属 ...
- JavaSE编码试题强化练习5
1.不使用函数实现字符串的翻转 /** * 1.不使用函数实现字符串的翻转 */ public class TestStringReverse { public static void main(St ...
- JavaSE编码试题强化练习4
1.编写一个Worker类,为Worker类添加相应的代码,使得Worker对象能正确放入TreeSet中.并编写相应的测试代码. /** * Worker类 */ public class Work ...
- JavaSE编码试题强化练习3
1.给20块钱买可乐,每瓶可乐3块钱,喝完之后退瓶子可以换回1块钱,问最多可以喝到多少瓶可乐. public class TestCirculation { public static void ma ...
- JavaSE编码试题强化练习2
1.编写递归算法程序:一列数的规则如下: 0.1.1.2.3.5.8.13.21.34...... 求数列的第40位数是多少. public class TestRecursion { public ...
- JavaSE面试题
JavaSE面试题 欢迎到我的Git仓库去提交您觉得优秀的内容! 1.是否可以从一个static方法内部发出对非static方法的调用? 不可以.当一个static方法被调用时,可能还没有创建任何实例 ...
- [002] - JavaSE面试题(二):基本数据类型与访问修饰符
第一期:Java面试 - 100题,梳理各大网站优秀面试题.大家可以跟着我一起来刷刷Java理论知识 [002] - JavaSE面试题(二):基本数据类型与访问修饰符 第1问:Java的数据类型有哪 ...
- JavaSE 面试题: 类初始化和实例初始化等
JavaSE 面试题 类初始化和实例初始化等 class Father { private int i = test(); private static int j = method(); stati ...
随机推荐
- mobilefacenet
insightface作者训练的mobileFaceNet: https://github.com/deepinsight/insightface/issues/214 ncnn的转换:http ...
- bzoj3510 首都 LCT 维护子树信息+树的重心
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=3510 题解 首先每一个连通块的首都根据定义,显然就是直径. 然后考虑直径的几个性质: 定义:删 ...
- oracle数据库架构
3.1 Client/Server Oracle 采取的是 Client/Server 架构. oracle 服务端分为两部分: Instance 实例 Database 数据库 实例, 又称为数据库 ...
- unittest-mock-from-import
https://stackoverflow.com/questions/11351382/mock-patching-from-import-statement-in-python
- cookie、session和中间件
目录 cookie和session cookie与session原理 cookie Google浏览器查看cookie Django操作cookie 获取cookie 设置cookie 删除cooki ...
- 【java工具类】AES加密解密
百度百科一下,AES:高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准.这个标准 ...
- POJ 2104 K-th Number ( 求取区间 K 大值 || 主席树 || 离线线段树)
题意 : 给出一个含有 N 个数的序列,然后有 M 次问询,每次问询包含 ( L, R, K ) 要求你给出 L 到 R 这个区间的第 K 大是几 分析 : 求取区间 K 大值是个经典的问题,可以使用 ...
- android读取xml
/*** 从config.xml中获取版本信息以及应用id* * @param urlPath* @return* @throws Exception*/public List getUpdateIn ...
- plt.imshow()为什么不能显示同时显两张照片
在编程的过程中发现plt.imshow()不能同时显示两张照片,如果有两条plt.imshow()语句处于一前一后的位置,那么程序运行后只会显示后面的图片.如果想让每一张图片都显示出来,需要在每一个p ...
- BZOJ 4939: [Ynoi2016]掉进兔子洞(莫队+bitset)
传送门 解题思路 刚开始想到了莫队+\(bitset\)去维护信息,结果发现空间不太够..试了各种奇技淫巧都\(MLE\),最后\(\%\)了发题解发现似乎可以分段做..这道题做法具体来说就是开\(3 ...