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. 64bit机器编译32bit汇编

    sudo apt-get install gcc-multilib sudo apt-get install g++-multilib gcc -m32  -S a.c -o a.s gcc -m64 ...

  2. php内置函数分析之array_fill_keys()

    PHP_FUNCTION(array_fill_keys) { zval *keys, *val, *entry; if (zend_parse_parameters(ZEND_NUM_ARGS(), ...

  3. zookeeper之一 安装和配置(单机+集群)

    这里我以zookeeper3.4.10.tar.gz来演示安装,安装到/usr/local/soft目录下. 一.单机版配置 1.安装和配置 #.下载 wget http://apache.fayea ...

  4. 如何判断系统是32位还是64位的linux系统

    如何判断系统是32位还是64位的linux系统 某日,需要下载个安装包,忽然忘记了自己的系统是32位还是64位的系统了,一时想不起来怎么查看时32位还是64位,呵呵,随便百度下,就发现有好多方法,这里 ...

  5. css3 宽度百分比减去固定宽度 无效问题

    一定要注意中间横线的间距才有效果 正确 width: calc(50% - 10px); 错误 width:calc(50%-10px);

  6. 【leetcode】1155. Number of Dice Rolls With Target Sum

    题目如下: You have d dice, and each die has f faces numbered 1, 2, ..., f. Return the number of possible ...

  7. BZOJ 3357: [Usaco2004]等差数列 动态规划

    Code: #include<bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin) # ...

  8. EOF和~

    输入包含多组数据 while(~scanf("%d",&n))<=>  while(scanf("%d",&n)!=EOF)  

  9. codevs 1098 均分纸牌 2002年NOIP全国联赛提高组 x

     时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold   题目描述 Description 有 N 堆纸牌,编号分别为 1,2,…, N.每堆上有若干张,但纸牌总数必 ...

  10. OpenCV Machine Learning (C++)

    /*M/////////////////////////////////////////////////////////////////////////////////////////// IMPOR ...