- 将来我爱刷题系列将与跃迁之路系列绑定,同步更新,即天天我都将运用跃迁之路的方法进行刻意练习。
- 总结套路,用乘法思路跃迁。
编写程序把这些IP按数值大小,从小到大排序并打印出来。
61.54.231.245 61.54.231.9 61.54.231.246 61.54.231.48 61.53.231.249java
package com.heima_xxxxxxxxx; import java.util.ArrayList; import java.util.Collections; public class Test5_4 { public static void main(String[] args) { mySort("61.54.231.245 61.54.231.9 61.54.231.246 61.54.231.48 61.53.231.249"); } public static void mySort(String str) { ArrayList<Integer> list = new ArrayList<>(); String[] arr1 = str.split(" "); for(String sTemp1 : arr1) { String[] arr2 = sTemp1.split("\\."); for(String sTemp2 : arr2) { list.add(Integer.parseInt(sTemp2)); } } Collections.sort(list); System.out.println(list); } }
package com.heima_xxxxxxxxx; import java.util.ArrayList; import java.util.Collections; public class Test5_4 { public static void main(String[] args) { mySort("61.54.231.245 61.54.231.9 61.54.231.246 61.54.231.48 61.53.231.249"); } public static void mySort(String str) { ArrayList<Integer> list = new ArrayList<>(); String[] arr1 = str.split("[^0-9]");//或使用\\D for(String s : arr1) { list.add(Integer.parseInt(s)); } Collections.sort(list); System.out.println(list); } }
书写一个类,类名为Itheima;code
类中有一个方法,方法名sameEnds;排序
给定一字符串,截取出该字符串开头和结尾相同的内容且不可重叠,并返回。
例如:("javaXYZjava") -> "java",不然返回空。索引
方法调用 | 指望值 |
---|---|
sameEnds("abXYab") | "ab" |
sameEnds("xx") | "x" |
sameEnds("xxx") | "x" |
package com.heima_6; public class Itheima_01 { public String sameEnds(String str) { //遍历字符 //索引0字符串 尝试 endsWith //为true 则截取返回 //为false pass //索引0 + 1字符串 尝试endsWith //。。。 //一直尝试到0+1+...+length-2索引字符(不能尝试倒数第1个了) String newStr = ""; for(int i = 0; i < str.length() - 1 ; i++) { newStr = newStr + str.charAt(i); if(str.endsWith(newStr)) { return newStr; } } return ""; } }
package com.heima_6; public class Test_01 { public static void main(String[] args) { Itheima_01 it = new Itheima_01(); System.out.println(it.sameEnds("abXYab")); System.out.println(it.sameEnds("xx")); System.out.println(it.sameEnds("xxx")); } }
书写一个类,类名为Itheima;rem
类中有一个方法,方法名sumNumbers;字符串
给定一字符串,求出如今字符串中的数字之和。
例如:sumNumbers("abc123xyz") → 123同步
方法调用 | 指望值 |
---|---|
sumNumbers("abc123xyz") | 123 |
sumNumbers("aa11b33") | 44 |
sumNumbers("7 11") | 18 |
package com.heima_3; public class Itheima_07 { public int sumNumbers(String str) { int sum = 0; String regex = "\\D+"; String[] arr = str.split(regex); for(String s : arr) { if(s.length() != 0) { sum = sum + Integer.parseInt(s); } } return sum; } }
package com.heima_3; public class Test_07 { public static void main(String[] args) { Itheima_07 it = new Itheima_07(); System.out.println(it.sumNumbers("abc123xyz")); System.out.println(it.sumNumbers("aa11b33")); System.out.println(it.sumNumbers("7 11")); } }
书写一个类,类名为Itheima;string
类中有一个方法,方法名withoutString;it
给定两个字符串参数base和remove,返回删除了remove字符串的base字符串(不区分大小写),
而且返回的base字符串不含有remove的重叠事例。
例如:("This is a FISH", "IS") -> "Th a FH" (注意Th和a之间有两个空格哦)
注意: 指望值里的一个空格能够表明多个空格.io
方法调用 指望值
withoutString("Hello there","llo") "He there"
withoutString("Hello there","e") "Hllo thr"
withoutString("Hello there","x") "Hello there"
package com.heima_6; public class Itheima_07 { public String withoutString(String base,String remove) { String newStr = base.replaceAll(remove,""); return newStr; } }
package com.heima_6; public class Itheima_07 { public String withoutString(String base,String remove) { for(int i = 0; i <= base.length() - remove.length(); i++) { String newStr = base.substring(i, i + remove.length()); if(newStr.equalsIgnoreCase(remove)) { base = base.replace(newStr, ""); } } return base; } }
package com.heima_6; public class Test_07 { public static void main(String[] args) { Itheima_07 it = new Itheima_07(); System.out.println(it.withoutString("Hello there", "llo")); System.out.println(it.withoutString("Hello there", "e")); System.out.println(it.withoutString("Hello there", "x")); } }