1、比较对象的大小
(1)Comparable接口
//Comparable接口的方式一旦一定,保证Comparable接口实现类的对象在任何位置都可以比较大小
public class Goods implements Comparable{
private String name;
private double price;
public Goods() {
}
public Goods(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Goods{" +
"name='" + name + '\'' +
", price=" + price +
'}';
}
//指明商品比较大小的方式:按照价格从低到高排序,再按照产品名称从高到低排序
@Override
public int compareTo(Object o) {
if(o instanceof Goods){
Goods goods = (Goods)o;
if(this.price > goods.price){
return 1;
}else if(this.price < goods.price){
return -1;
}else{
return -this.name.compareTo(goods.name);
}
}
throw new RuntimeException("传入的数据类型不一致!");
}
}
Comparable接口的使用举例: 自然排序
1.像String、包装类等实现了Comparable接口,重写了compareTo(obj)方法,给出了比较两个对象大小的方式。
2.像String、包装类重写compareTo()方法以后,进行了从小到大的排列
3. 重写compareTo(obj)的规则:
如果当前对象this大于形参对象obj,则返回正整数,
如果当前对象this小于形参对象obj,则返回负整数,
如果当前对象this等于形参对象obj,则返回零。
4. 对于自定义类来说,如果需要排序,我们可以让自定义类实现Comparable接口,重写compareTo(obj)方法。
在compareTo(obj)方法中指明如何排序
(2)Comparator接口
//Comparator接口属于临时性的比较
String[] arr = new String[]{"AA","CC","KK","MM","GG","JJ","DD"};
Arrays.sort(arr,new Comparator(){
//按照字符串从大到小的顺序排列
@Override
public int compare(Object o1, Object o2) {
if(o1 instanceof String && o2 instanceof String){
String s1 = (String) o1;
String s2 = (String) o2;
return -s1.compareTo(s2);
}
throw new RuntimeException("输入的数据类型不一致");
}
});
System.out.println(Arrays.toString(arr));
Comparator接口的使用:定制排序
1.背景:
当元素的类型没有实现java.lang.Comparable接口而又不方便修改代码,
或者实现了java.lang.Comparable接口的排序规则不适合当前的操作,
那么可以考虑使用 Comparator 的对象来排序
2.重写compare(Object o1,Object o2)方法,比较o1和o2的大小:
如果方法返回正整数,则表示o1大于o2;
如果返回0,表示相等;
返回负整数,表示o1小于o2。
2、字符串反转
将字符串中指定部分进行反转。比如“abcdefg”反转为”abfedcg”
//方式一:转换为char[]
public String reverse(String str,int startIndex,int endIndex){
if(str != null){
char[] arr = str.toCharArray();
for(int x = startIndex,y = endIndex;x < y;x++,y--){
char temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
return new String(arr);
}
return null;
}
//方式二:使用String的拼接
public String reverse1(String str,int startIndex,int endIndex){
if(str != null){
//第1部分
String reverseStr = str.substring(0,startIndex);
//第2部分
for(int i = endIndex;i >= startIndex;i--){
reverseStr += str.charAt(i);
}
//第3部分
reverseStr += str.substring(endIndex + 1);
return reverseStr;
}
return null;
}
//方式三:使用StringBuffer/StringBuilder替换String
public String reverse2(String str,int startIndex,int endIndex){
if(str != null){
StringBuilder builder = new StringBuilder(str.length());
//第1部分
builder.append(str.substring(0,startIndex));
//第2部分
for(int i = endIndex;i >= startIndex;i--){
builder.append(str.charAt(i));
}
//第3部分
builder.append(str.substring(endIndex + 1));
return builder.toString();
}
return null;
}
3、字符串出现的次数
获取一个字符串在另一个字符串中出现的次数。
比如:获取“ab”在 “abkkcadkabkebfkaabkskab” 中出现的次数
public int getCount(String mainStr,String subStr){
int mainLength = mainStr.length();
int subLength = subStr.length();
int count = 0;
int index = 0;
if(mainLength >= subLength){
while((index = mainStr.indexOf(subStr,index)) != -1){
count++;
index += subLength;
}
return count;
}else{
return 0;
}
}