1.九九乘法表
public static void main(String[] args) {
for (int i = 0; i <= 9; i++){
for (int j = 1; j <= i; j++){
System.out.print(j + "*" + i + "=" + (i*j) + "\t");
}
System.out.println();
}
}
2.输出 100 以内所有的素数
public static void main(String[] args) {
int count = 0;
for (int i = 2; i < 100; i++){
boolean flag = true;
for (int j = 2; j < i; j++){
if(i % j == 0){
flag = false;
break;
}
}
if (flag){
count++;
System.out.print(i + " ");
}
}
System.out.println();
System.out.println("100 以内的素数个数是:"+count);
}
3.输出1000以内所有的水仙花数
public static void main(String[] args) {
int ge,shi,bai;
int count = 0;
for (int i = 100; i < 1000; i++){
ge = i % 10;
shi = i/10%10;
bai = i/100;
int result = (int)(Math.pow(bai,3)+Math.pow(shi,3)+Math.pow(ge,3));
if(i == result){
count ++;
System.out.print(result+" ");
}
}
System.out.println();
System.out.println("1000以内所有的水仙花数的个数:"+count);
}
4.统计不同类型字符的个数
public static void main(String[] args) {
String str = "adsf eQW&ER @T12 %3~4 $567 !#890";
char[] chars = str.toCharArray();
int englishCount = 0;
int spaceCount = 0;
int digitCount = 0;
int otherCount = 0;
for (int i = 0; i<str.length(); i++){
if (Character.isLetter(chars[i])){
englishCount++;
continue;
}
if (Character.isDigit(chars[i])){
digitCount++;
continue;
}
if (Character.isSpaceChar(chars[i])){
spaceCount++;
continue;
}
otherCount++;
}
System.out.println("英语字母个数为:"+englishCount);
System.out.println("数字个数为:"+digitCount);
System.out.println("空格个数为:"+spaceCount);
System.out.println("其他字符个数为:"+otherCount);
}
5.计算1-100之间的和
public static void main(String[] args) {
System.out.println("1-100之间的和为:"+sum(1000));
}
public static int sum(int value){
if (value == 1){
return 1;
}
return value+sum(value-1);
}
6.编程输出只出现过一次的数
public static void main(String[] args) {
int[] arr = new int[]{1,1,23,23,34,43,43,45,45,65,67,65,67,78};
for (int i = 0; i < arr.length; i++) {
int num = 0;
for (int j = 0; j < arr.length; j++) {
if (arr[i] == arr[j]) {
num++;
}
}
if (num == 1) {
System.out.println("只出现过一次的数为:"+arr[i]);
}
}
}
7.冒泡排序
public static void main(String[] args) {
int[] arr = {78,56,97,23,57,43,65,3,12,90};
System.out.print("排序之前的:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
int temp;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length-1-i; j++) {
if (arr[j] > arr[j+1]) {
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
System.out.println();
System.out.print("排序之后的:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
8.快速排序
public static void main(String[] args) {
int[] arr = {78,56,97,23,57,43,65,3,12,90};
System.out.print("排序之前的:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
正在排序
System.out.println();
System.out.println("正在排序。。。");
quickSort(arr,0,arr.length-1);
System.out.println();
System.out.print("排序之后的:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
public static void quickSort(int[] arr, int low, int high) {
int pivot,pos,t;
if (low < high) {
pos = low;
pivot = arr[pos];
for (int i = low+1; i <= high; i++) {
if (arr[i]<pivot){
pos++;
t = arr[pos];
arr[pos] = arr[i];
arr[i] = t;
}
}
t = arr[low];
arr[low] = arr[pos];
arr[pos] = t;
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+"\t");
}
System.out.println();
排序左半部分
quickSort(arr,low,pos-1);
排序右半部分
quickSort(arr,pos+1,high);
}
}
9.选择排序
public static void main(String[] args) {
int[] arr = {78,56,97,23,57,43,65,3,12,90};
System.out.print("排序之前的:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
正在排序
System.out.println();
System.out.println("正在排序。。。");
selectSort(arr);
System.out.println();
System.out.print("排序之后的:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
public static void selectSort(int[] arr) {
int temp;
for (int j = 0; j < arr.length; j++) {
int min = arr[j];
int minIndex = j;
for (int k = j; k < arr.length; k++) {
if (min > arr[k]){
min = arr[k];
minIndex = k;
}
}
temp = arr[j];
arr[j] = arr[minIndex];
arr[minIndex] = temp;
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+"\t");
}
System.out.println();
}
}
10.插入排序
public static void main(String[] args) {
int[] arr = {78,56,97,23,57,43,65,3,12,90};
System.out.print("排序之前的:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
正在排序
System.out.println();
System.out.println("正在排序。。。");
insertSort(arr);
System.out.println();
System.out.print("排序之后的:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
private static void insertSort(int[] arr) {
int i,j,t;
for (i = 1; i < arr.length; i++) {
if (arr[i] < arr[i-1]) {
t = arr[i];
for (j = i-1; j >= 0 && t < arr[j]; j--) {
arr[j+1] = arr[j];
}
arr[j+1] = t;
for (int k = 0; k < arr.length; k++) {
System.out.print(arr[k]+"\t");
}
System.out.println();
}
}
}
11.希尔排序
public static void main(String[] args) {
int[] arr = {78,56,97,23,57,43,65,3,12,90};
System.out.print("排序之前的:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
正在排序
System.out.println();
System.out.println("正在排序。。。");
shellSort(arr);
System.out.println();
System.out.print("排序之后的:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
private static void shellSort(int[] arr) {
int j, temp;
for (int incr = arr.length/2; incr > 0; incr/=2) {
for (int i = incr; i < arr.length; i++) {
temp = arr[i];
for (j = i; j >= incr; j -= incr) {
if (temp < arr[j - incr]){
arr[j] = arr[j - incr];
}else{
break;
}
}
arr[j] = temp;
for (int k = 0; k < arr.length; k++) {
System.out.print(arr[k]+"\t");
}
System.out.println();
}
}
}
12.二分排序
public static void main(String[] args) {
int[] arr = {78,56,97,23,57,43,65,3,12,90};
System.out.print("排序之前的:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
正在排序
System.out.println();
System.out.println("正在排序。。。");
binarySort(arr);
System.out.println();
System.out.print("排序之后的:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
private static void binarySort(int[] arr) {
for (int i = 1; i < arr.length; i++) {
int temp = arr[i]; 要插入的第i个元素
int low = 0;
int high = i - 1; 插入目标元素的前 i-1 个元素
int mid = -1;
while (low <= high) {
mid = low + (high - low) / 2;
if (arr[mid] > temp) {
high = mid - 1;
} else { 元素相同时,也插入在后面的位置
low = mid + 1;
}
}
目标位置 之后的元素 整体移动一位
for(int j = i - 1; j >= low; j--) {
arr[j + 1] = arr[j];
}
arr[low] = temp;
for (int k = 0; k < arr.length ; k++) {
System.out.print(arr[k]+" ");
}
System.out.println();
}
}
13.归并排序
public static void main(String[] args) {
int[] arr = {78,56,97,23,57,43,65,3,12,90};
System.out.print("排序之前的:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
正在排序
System.out.println();
System.out.println("正在排序。。。");
mergeSort(arr, 0, arr.length-1);
System.out.println();
System.out.print("排序之后的:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
private static void mergeSort(int[] arr, int start, int end) {
if (start < end){
int m = (start + end) / 2;
mergeSort(arr, start, m);
mergeSort(arr, m+1, end);
merge(arr, start, m, end);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + "\t");
}
System.out.println();
}
}
private static void merge(int[] arr, int start, int m, int end) {
int length = end -start + 1;
int temp[] = new int [length];
int i = start;
int j = m + 1;
int k = 0;
while (i <= m && j <= end){
if (arr[i] < arr[j]){
temp[k++] = arr[i++];
}else{
temp[k++] = arr[j++];
}
}
while (i <= m){
temp[k++] = arr[i++];
}
while (j <= end){
temp[k++] = arr[j++];
}
k = 0;
for (int t = start; t <= end; t++){
arr[t] = temp[k++];
}
}
14.实现让 3 个线程按顺序执行
public static void main(String[] args) throws Exception{
Thread t1 = new Thread(() ->{
try {
Thread.sleep(2000);
}catch (Exception e){
e.printStackTrace();
}
System.out.println("线程1执行完成。。。");
});
Thread t2 = new Thread(() ->{
try {
Thread.sleep(1000);
}catch (Exception e){
e.printStackTrace();
}
System.out.println("线程2执行完成。。。");
});
Thread t3 = new Thread(() ->{
System.out.println("线程3执行完成。。。");
});
t1.start();
t1.join();
t2.start();
t2.join();
t3.start();
}
15.实现一个多线程死锁
private static Object lock1 = new Object();
private static Object lock2 = new Object();
public static void main(String[] args) {
new Thread(() -> {
System.out.println("thread1 get lock1");
synchronized (lock1){
try{
Thread.sleep(1000);
}catch (Exception e){
e.printStackTrace();
}
synchronized (lock2){
System.out.println("thread1 get lock2");
}
System.out.println("thread1 end");
}
}).start();
new Thread(() -> {
System.out.println("thread2 get lock2");
synchronized (lock2){
try{
Thread.sleep(1000);
}catch (Exception e){
e.printStackTrace();
}
synchronized (lock1){
System.out.println("thread2 get lock1");
}
System.out.println("thread2 end");
}
}).start();
}
16.实现删除 list 集合的元素
public static void main(String[] args) {
List<String> list = new ArrayList();
list.add("sdfs");
list.add("adff");
list.add("werwer");
list.add("adeww");
list.add("rgdfg");
System.out.println(list);
for (Iterator<String> ite = list.iterator(); ((Iterator) ite).hasNext();){
String str = ite.next();
if (str.contains("sdfs")){
ite.remove();
}
}
System.out.println(list);
}