11.个别程序题

 1 package com.array;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 import java.util.Random;
 6 
 7 /**
 8  * @Desciption:向 int 数组随机插入 100 个数
 9  * @author swifthua
10  * @date 2017年10月20日 下午9:08:29
11  */
12 /*
13  * 题目介绍:产生一个 int 数组,长度为 100,并向其中随机插入 1-100,并且不能重复。
14  */
15 public class IntArray {
16 
17     public static void main(String[] args) {
18         int[] obj = new int[100]; // 声明一个100长度的数组
19         List<Integer> list = new ArrayList<Integer>();
20         for (int i = 1; i <= 100; i++)
21             list.add(i); // 我把100个数存到集合中
22 
23         for (int i = 0; i < 100; i++) {
24             int index = new Random().nextInt(list.size()); // 随机取一个小于当前集合长度的下标
25             obj[i] = list.get(index);// 取出集合中的数据放到数组里
26             list.remove(index);// 集合中删除此下标
27         }
28         for (int i = 0; i < 100; i++) { // 打印输出
29             if (i % 10 == 0) {
30                 System.out.println();
31             } else {
32                 System.out.print(obj[i] + " ");
33             }
34         }
35     }
36 
37 }
 1 package com.datatype;
 2 
 3 /**
 4  * @Desciption:递归逆序输出一个 int 类型数
 5  * @author swifthua
 6  * @date 2017年10月20日 下午9:12:55
 7  */
 8 /*
 9  * 原题:用递归算法对输入一个整形数,然后逆序输出,输出的必须是字符串。
10  */
11 public class IntTest1 {
12     public static String reverse(int a) {
13         if (a < 0) {
14             return "请输入一个正整数..";
15         }
16         if (a < 10)
17             return Integer.toString(a);
18         int last = a - (a / 10) * 10;// 获取未位
19         return Integer.toString(last) + reverse(a / 10);// 递归输出最后一位和前面的倒序数字
20     }
21 
22     public static void main(String[] args) {
23         String str = reverse(12342);
24         System.out.println(str);
25     }
26 }
 1 package com.string;
 2 
 3 /**
 4  * @Desciption:截取字符串
 5  * @author swifthua
 6  * @date 2017年10月20日 下午8:55:59
 7  */
 8 /*
 9   编程:编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。但是要保证汉字不被截半个,如"我ABC"4,
10   应该截为"我AB",输入"我ABC汉DEF",6,应该输出为"我ABC"而不是"我ABC+汉的半个"。
11  */
12 public class CutString {
13     public static void split(String source, int num) {
14         int k = 0;
15         StringBuffer sbf = new StringBuffer("");
16         for (int i = 0; i < source.length(); i++) {
17             byte[] b = (source.charAt(i) + "").getBytes();
18             k = k + b.length;
19             if (k > num)
20                 break;
21             sbf.append(source.charAt(i));
22         }
23         System.out.println(sbf.toString());
24     }
25 
26     public static void main(String[] args) {
27         String SOURCE = "我ABC汉DEF";
28         int NUM = 5;
29         CutString.split(SOURCE, NUM);
30     }
31 }
 1 package com.search;
 2 
 3 /**
 4  * 二分查找
 5  * @author swifthua
 6  * @date 2017年10月21日 下午1:12:10
 7  */
 8 /*
 9  * 算法的思想是对于已经有序的数组,查找一个随机数字时,每次取数组的中间数与目标数比较便可。
10  */
11 public class BinarySearch {
12 
13     public static int find(int[] array, int objKey) {
14         int start = 0;
15         int end = array.length - 1;
16         while (start <= end) {
17             int middle = (start + end) / 2; // 找出中间位
18             if (objKey < array[middle]) {
19                 end = middle - 1;
20             } else if (objKey > array[middle]) {
21                 start = middle + 1;
22             } else {
23                 return middle;
24             }
25         }
26         return -1;
27     }
28 
29     public static void main(String[] args) {
30         int array[]=new int[]{1,2,4,12,25,36,48,58,64,99};
31         System.out.println(find(array,64));
32         System.out.println(find(array,16));
33     }
34 
35 }
 1 package com.thread;
 2 
 3 /**
 4  * @Desciption:多线程编程
 5  * @author swifthua
 6  * @date 2017年10月20日 下午9:04:04
 7  */
 8 /*
 9  * 设计 4 个线程,其中两个线程每次对 j 增加 1,另外两个线程对 j 每次减少 1,写出程序。
10  */
11 public class ThreadTest {
12     private int j;
13 
14     public static void main(String args[]) {
15         ThreadTest tt = new ThreadTest();
16         Inc inc = tt.new Inc();
17         Dec dec = tt.new Dec();
18         for (int i = 0; i < 2; i++) {
19             Thread t = new Thread(inc);
20             t.start();
21             t = new Thread(dec);
22             t.start();
23         }
24     }
25 
26     private synchronized void inc() {
27         j++;
28         System.out.println(Thread.currentThread().getName() + "-inc:" + j);
29     }
30 
31     private synchronized void dec() {
32         j--;
33         System.out.println(Thread.currentThread().getName() + "-dec:" + j);
34     }
35 
36     class Inc implements Runnable {
37         public void run() {
38             for (int i = 0; i < 100; i++) {
39                 inc();
40             }
41         }
42     }
43 
44     class Dec implements Runnable {
45         public void run() {
46             for (int i = 0; i < 100; i++) {
47                 dec();
48             }
49         }
50     }
51 
52 }
原文地址:https://www.cnblogs.com/swifthua/p/7704594.html