学习笔记
十进制转二进制 十进制转换成二进制 Integer.toBinaryString(num);
package list;
import java.util.ArrayList; import java.util.List;
public class mylist { public static void main(String[] args) { List list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
list.add("e");
list.add(5,"f");
list.set(0,"qqq");
System.out.println(list);
System.out.println(list.get(5));
}
}
//题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
public class jva1209 { public static void main(String[] args) { //int [] a = {1,2,3,4}; for (int i = 1; i < 6; i++) { for (int j = 1; j < 5; j++) { for (int k = 1; k < 5; k++) { if(i != j && i != k && j != k){ System.out.println("可以组成的三位数有:" + i + j + k); }
}
}
}
}
}
20211213
//给定任意一个字符串“today is a special day”,长度为任意,要求找出其出现次数最多的字符及计算次数。(可以用HashMap,HashSet,Collections实现)
package haspmap;
import java.awt.print.PrinterGraphics; import java.util.*;
public class hashmap { public static void main(String[] args) { countChar(); }
public static void countChar() {
Scanner scan = new Scanner(System.in);
System.out.println("请输入第一行字符串:");
String str = scan.nextLine();
HashMap<Character,Integer>map = new HashMap<Character,Integer>();
for (int i = 0; i < str.length(); i++) {
Character ch = new Character(str.charAt(i));
Integer val = new Integer(1);
Integer oldVal = map.put(ch,val);
if(oldVal != null){
map.put(ch,oldVal + 1);
}
}
System.out.println(map);
int max = 0;
Character ch1 = null;
Set<Character> set = new HashSet<Character>();
set = map.keySet();
Iterator<Character>it = set.iterator();
while (it.hasNext()){
Character ch = (Character) it.next();
Integer pos = map.get(ch).intValue();
if(pos > max){
max = pos;
ch1 = ch;
}
}
System.out.println("字符串" + str + "中出现最多的字符是:" + ch1 + "\n" + "出现次数为:" + max);
}
}
输入字符串逆序输出
import java.util.*;
public class java1223 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.next();
reverseString1(str);
}
private static String reverseString1(String str) {
char[] chr = str.toCharArray();
for (int i = chr.length - 1; i >= 0; i--) {
System.out.print(chr[i]);
}
System.out.println("\t");
return str;
}
}
冒泡排序 public class bubblesort1219 { public static void main(String[] args) { int [] arr = {6,3,8,2,9,1}; System.out.println("排列前的数组为: "); for(int num:arr){ System.out.print(num + " "); } for (int i = 0; i < arr.length-1; i++) { for (int j = 0; j < arr.length-1-i; j++) { if(arr[j]>arr[j+1]){ int tem = arr[j]; arr[j] = arr[j+1]; arr[j+1] = tem; } } } System.out.println(); System.out.println("排序后的数组为: "); for(int num:arr){ System.out.print(num + " "); } } }
二分法 import java.util.Arrays;
public class java1219 { public static void main(String[] args) { int[] arr = {20,34,13,67,12,41,44,69}; int searchWord = 20; Arrays.sort(arr); System.out.println(Arrays.toString(arr)); System.out.println(searchWord + "元素的索引:" + binarySearch(arr,searchWord));
System.out.println("==========");
int errorWord = 66;
System.out.println(errorWord + "元素的索引:" + binarySearch(arr,errorWord));
}
public static int binarySearch(int[] arr, int value){
int low = 0;
int high = arr.length - 1;
while(low <= high){
int middle = (low + high)/2;
if(value == arr[middle]){
return middle;
}
if (value > arr[middle]){
low = middle + 1;
}
if (value < arr[middle]){
high = middle - 1;
}
}
return -1;
}
}