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. bzoj4810 [Ynoi2017]由乃的玉米田 莫队+bitset(+数论)

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4810 题解 看数据范围和题目名字应该是根号算法. 因为询问除了区间外,还有第 \(3\) 个参 ...

  2. Linux服务器调优

    Linux内核参数 http://space.itpub.net/17283404/viewspace-694350 net.ipv4.tcp_syncookies = 表示开启SYN Cookies ...

  3. mac下安装 rabbitMq

    1.安装HomeBrew,如果已经安装这一步跳过. 2.用brew install rabbitmq指令即可进行rabbitmq服务的自动安装. 3.安装完成之后会出现一下提示:   rabbit安装 ...

  4. unittest-mock-from-import

    https://stackoverflow.com/questions/11351382/mock-patching-from-import-statement-in-python

  5. python-hasattr()、getattr()、setattr()函数的使用

    python中hasattr().getattr().setattr()函数 class A(): name = 'python' def __init__(self): setattr(self._ ...

  6. webstorm注册码,亲测2016.1.1版

    打开webstorm,点击帮助,注册 注册时,在打开的License Activation窗口中选择“License server”,在输入框输入下面的网址: http://idea.iteblog. ...

  7. django快速搭建blog

    python版本:3.5.4: Django版本:2.0 创建项目 创建mysite项目和 blog应用: django-admin startproject mysite # 创建mysite项目 ...

  8. poj 3613Cow Relays

    Description For their physical fitness program, N (2 ≤ N ≤ 1,000,000) cows have decided to run a rel ...

  9. I - Rake It In

    题目链接:https://nanti.jisuanke.com/t/A1538 题意:给一个4*4的方阵,k个回合,a和b轮流选一个2*2的矩阵和,a要使和最大,b要使和最小,选完后2*2矩阵要逆时针 ...

  10. 170830-关于JdbcTemplate的知识点

    1.Spring提供的一个操作数据库的技术JdbcTemplate,是对Jdbc的封装.语法风格非常接近DBUtils. JdbcTemplate可以直接操作数据库,加快效率,而且学这个JdbcTem ...