public class Test01 {
//按照字节数截取字符串
public static void main(String[] args) {
String str="我ABC";
String str1="我ABC汉DEF";
subStringByBytes(str, 4);
subStringByBytes(str1, 6);
}
public static void subStringByBytes(String str,int len){
if(str==null){
return;
}
if(len>str.length()){
len=str.length();
}
byte [] bytes=str.getBytes();
//对是否出现截半做分类处理,如果出现了截半情况,则asc码值小于0
if(bytes[len]<0) {
String result = new String(bytes,0, len--);
System.out.println("出现了截半情况,结果为:" + result);
} else {
String result = new String(bytes,0,len);
System.out.println("没有出现截半情况,结果为:" + result);
}
}
}
public class Test02 {
public static void main(String[] args) {
method(101,200);
}
public static void method(int start,int end){
int count=0;
boolean flag=false;
for(int i=start;i<=end;i++){
for(int j=2;j<i;j++){
if(i%j==0){
flag=false;
break;
}else{
flag=true;
}
}
if(flag){
count++;
System.out.println(i);
}
}
System.out.println(start+"到"+end+"之间有"+count+"个素数");
}
}
3.判断一个数是不是2的次方
public class Test03 {
public static void main(String[] args) {
boolean b = method(16);
if(b){
System.out.println("是");
}else{
System.out.println("不是");
}
}
public static boolean method(int num){
boolean flag = false;
if((num&(num-1))==0&&num>0){
flag=true;
}
return flag;
}
// public static boolean method(int num){
// int k = 1;
// boolean flag = false;
// while(k <= num) {
// if(num == k){
// flag = true;
// }
// k = k * 2;
// }
//
// return flag;
//
// }
}
public class Test04 {
public static void main(String[] args) {
int num=24;
if(num%2==0&&num>0){
method(num);
}
}
public static void method(int num){
if(num!=2){
List<Integer> primes = getPrime(num);
for (int i = 0; i < primes.size(); i++) {
for (int j = 0; j < primes.size(); j++) {
if(primes.get(i)+primes.get(j)==num){
System.out.println(primes.get(i)+"+"+primes.get(j)+"="+num);
}
}
}
}else if(num==2){
System.out.println("2是素数,且不能拆分");
}
}
public static List<Integer> getPrime(int num){
List<Integer> list=new ArrayList<>();
for(int i=2;i<=num;i++){
boolean b=true;
for(int j=2;j<i;j++){
if(i%j==0){
b=false;
break;
}
}
if (b) {
list.add(i);
}
}
return list;
}
}
5.代码实现jdbc,并进行基本的操作
public class Test05 {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");
Statement statement = conn.createStatement();
String sql="select s_id from student";
ResultSet rs = statement.executeQuery(sql);
while(rs.next()){
int id = rs.getInt(1);
System.out.println(id);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class Test06 {
public static void main(String[] args) {
String str="中国 aadf 的111萨 bbb 菲的 zz 萨菲";
Map<String, Integer> map = method(str);
Set<Entry<String,Integer>> set=map.entrySet();
for(Entry<String,Integer> e:set){
System.out.println(e.getKey()+":"+e.getValue());
}
}
public static Map<String,Integer> method(String str){
Map<String,Integer> map=new HashMap<>();
for(int i=0;i<str.length();i++){
char c=str.charAt(i);
Integer num=map.get(String.valueOf(c));
if(num==null){
num=1;
}else{
num=num+1;
}
map.put(String.valueOf(c), num);
}
return map;
}
}
7.有数组 a[n],将数组元素顺序颠倒
public class Test07 {
public static void main(String[] args) {
int [] a={1,2,3,4,5};
int[] newArray = method(a);
System.out.println(Arrays.toString(newArray));
}
public static int[] method(int [] a){
int temp;
int len=a.length;
for(int i=0;i<len/2;i++){
temp=a[i];
a[i]=a[len-1-i];
a[len-1-i]=temp;
}
return a;
}
}
8.金额转换,阿拉伯数字的金额转换成中国传统的形式如:(¥1011)->(一千零一拾一元整)输出。
public class Test08 {
public static void main(String[] args) {
int money=2350230;
String renMingBi = method(money);
System.out.println(renMingBi);
}
public static String method(int money){
char[] data = new char[]{'零','壹','贰','叁','肆','伍','陆','柒','捌','玖'};
char[] units = new char[]{'元','拾','佰','仟','万','拾','佰','仟','亿'};
StringBuffer sb=new StringBuffer();
int unit=0;
while(money!=0){
sb.insert(0, units[unit++]);
int num=money%10;
sb.insert(0, data[num]);
money/=10;
}
return sb.toString();
}
}
public class Test9 {
public static void main(String[] args){
String str = "abc";
List<String> result = new ArrayList<>();
getAllList(result, str.toCharArray(), 0);
for(String each : result){
System.out.println(each);
}
}
public static void getAllList(List<String> result, char[] strs, int begin){
if(begin == strs.length-1){
result.add(String.valueOf(strs));
}
for(int i = begin; i < strs.length; i++){
swap(strs, i, begin);
getAllList(result, strs, begin+1);
swap(strs, i, begin);
}
}
private static void swap(char[] strs, int index1, int index2) {
char temp = strs[index1];
strs[index1] = strs[index2];
strs[index2] = temp;
}
}
10.判断一个字符数组元素是否重复
public class Test11 {
public static void main(String[] args) {
char[] chars={'1','3','3'};
System.out.println(method(chars));
}
public static boolean method(char [] chars){
boolean flag=true;
StringBuffer sb=new StringBuffer();
for(int i=0;i<chars.length;i++){
sb.insert(0, chars[i]);
}
String str=sb.toString();
for(int i=0;i<str.length();i++){
if(i!=str.lastIndexOf(str.charAt(i))){
flag=false;
}
}
return flag;
}
}