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. PAT Advanced 1036 Boys vs Girls (25 分)

    This time you are asked to tell the difference between the lowest grade of all the male students and ...

  2. 02MySQL数据库

    1.MySQL启动和关闭 2.登录MySQL数据库 MySQL是一个需要账户名密码登录的数据库,登陆后使用,它提供了一个默认的root账号,使用安装时设置的密码即可登录. 格式1:cmd>  m ...

  3. Insomni'hack teaser 2019 - Pwn - 1118daysober

    参考链接 https://ctftime.org/task/7459 Linux内核访问用户空间文件:get_fs()/set_fs()的使用 漏洞的patch信息 https://maltekrau ...

  4. git分支管理和工作流规范:不同场景细化和演示

    https://www.iteye.com/blog/qqtalk-2415889 前两篇介绍了 git基本概念 和 具体的规范,本篇针对不同的使用场景做演示. 分支 分支命名 master 分支名称 ...

  5. 前端之JavaScript:JS之DOM对象二

    继续JS之DOM对象二 前面在JS之DOM中我们知道了属性操作,下面我们来了解一下节点操作.很重要!! 一.节点操作 创建节点:var ele_a = document.createElement(' ...

  6. git:Git fetch和git pull的区别, 解决Git报错:error: You have not concluded your merge (MERGE_HEAD exists).

    Git fetch和git pull的区别, 解决Git报错:error: You have not concluded your merge (MERGE_HEAD exists). 解决办法一:保 ...

  7. 【ZJOI2008】树的统计

    题目 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w. 我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t II. QMAX u v: ...

  8. vue addRoutes路由动态加载

    需求:增加权限控制,实现不同角色显示不同的路由导航 思路:每次登陆后请求接口返回当前角色路由 核心方法:vue-router2.2.0的addRoutes方法 + vuex 以下是我实现的获取菜单路由 ...

  9. SQL server 表copy 到别一张表

    SQL server  表copy 到别一张表 ------------------ INSERT INTO  表名 (表字段)   SELECT  表1字段 FROM 表名2: ---------- ...

  10. a标签禁止跳转

    方法一: <a href="javascipt:void(0)>....</a>