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的更多相关文章

  1. JavaSE编码试题强化练习1

    1. 编写应用程序,创建类的对象,分别设置圆的半径.圆柱体的高,计算并分别显示圆半径.圆面积.圆周长,圆柱体的体积. /** * 定义父类--圆类 */ public class Circle { / ...

  2. JavaSE编码试题强化练习7

    1.编写应用程序,创建类的对象,分别设置圆的半径.圆柱体的高,计算并分别显示圆半径.圆面积.圆周长,圆柱体的体积. /** * 圆类 */ public class Circle { /** * 类属 ...

  3. JavaSE编码试题强化练习5

    1.不使用函数实现字符串的翻转 /** * 1.不使用函数实现字符串的翻转 */ public class TestStringReverse { public static void main(St ...

  4. JavaSE编码试题强化练习4

    1.编写一个Worker类,为Worker类添加相应的代码,使得Worker对象能正确放入TreeSet中.并编写相应的测试代码. /** * Worker类 */ public class Work ...

  5. JavaSE编码试题强化练习3

    1.给20块钱买可乐,每瓶可乐3块钱,喝完之后退瓶子可以换回1块钱,问最多可以喝到多少瓶可乐. public class TestCirculation { public static void ma ...

  6. JavaSE编码试题强化练习2

    1.编写递归算法程序:一列数的规则如下: 0.1.1.2.3.5.8.13.21.34...... 求数列的第40位数是多少. public class TestRecursion { public ...

  7. JavaSE面试题

    JavaSE面试题 欢迎到我的Git仓库去提交您觉得优秀的内容! 1.是否可以从一个static方法内部发出对非static方法的调用? 不可以.当一个static方法被调用时,可能还没有创建任何实例 ...

  8. [002] - JavaSE面试题(二):基本数据类型与访问修饰符

    第一期:Java面试 - 100题,梳理各大网站优秀面试题.大家可以跟着我一起来刷刷Java理论知识 [002] - JavaSE面试题(二):基本数据类型与访问修饰符 第1问:Java的数据类型有哪 ...

  9. JavaSE 面试题: 类初始化和实例初始化等

    JavaSE 面试题 类初始化和实例初始化等 class Father { private int i = test(); private static int j = method(); stati ...

随机推荐

  1. Netty学习第三章 Linux网络编程使用的I/O模型

    一.同步阻塞IO:blocking IO(BIO) 1.过程分析: 当进程进行系统调用时,内核就会去准备数据,当数据准备好后就复制数据到内核缓冲器,复制完成后将数据拷贝到用户进程内存,整个过程都是阻塞 ...

  2. java实现一个简单的计数器

    package com.fengunion.sf; import org.junit.platform.commons.util.StringUtils; import java.util.HashM ...

  3. gitpython 操作

    gitpython 获取仓库远程分支 https://blog.csdn.net/laiyaoditude/article/details/86218508 python操作git https://w ...

  4. ping/curl

    ping查看网络连通情况 curl查看接口可用情况

  5. ESP8266-12F

    读者可以把ESP8266当做Arduino+WiFi功能来开发 ESP8266模块支持STA/AP/STA+AP 三种工作模式: STA 模式:ESP8266模块通过路由器连接互联网,手机或电脑通过互 ...

  6. day3 ord,chr,random,string

    day3复习 >>> for i in range(10): ... if i == 3: ... break ... print(i) ... 0 1 2 >>> ...

  7. jquery每次动态加载dom,绑定事件会多一次,

    jquery绑定事件,每次动态加载dom,绑定的事件会加1,比如动态加载dom5次,点那个点击事件会弹出5次 解决办法就是在每次绑定之前解绑定. $('.seek-footer .btn1').off ...

  8. BZOJ 1733: [Usaco2005 feb]Secret Milking Machine 神秘的挤奶机 网络流 + 二分答案

    Description Farmer John is constructing a new milking machine and wishes to keep it secret as long a ...

  9. ndn挖坑记(二)

    目录 如何使用ndnSIM运行自己的仿真实验 基本要点 开始动手 BUG记录 如何使用ndnSIM运行自己的仿真实验 基本要点 仿真场景可以在NS-3目录下的scratch/ or src/ndnSI ...

  10. spting-security入门

    spting-security入门 11-