java笔试--十进制数字二进制的个数(比较器)
程序员文章站
2022-07-15 09:29:16
...
Integer二进制函数:
- public static int bitCount(int i):数字i的二进制中1的个数
- public static String toBinaryString(int i):将十进制i转化为二进制
- public static String toOctalString(int i):将十进制i转化为八进制
- public static String toHexString(int i):将十进制i转化为十六进制
方法一:将元素于二进制个数相关联。
可以将每一个数字的二进制中1的数量于数字本身相关联,所以可以采用在数字原本的基础上Integer.bitCount(arr[i])*10001+arr[i](10001是因为数组的最大值只能是10000.)。在进行排序。
class Solution {
public int[] sortByBits(int[] arr) {
for(int i=0;i<arr.length;i++){
arr[i]=Integer.bitCount(arr[i])*10001+arr[i];
}
Arrays.sort(arr);
for(int i=0;i<arr.length;i++){
arr[i]=arr[i]%10001;
}
return arr;
}
}
方法二:实现自定义的构造器
public static int[] sortByBits(int[] arr) {
Integer[] a=new Integer[arr.length];
for(int i=0;i<arr.length;i++){
a[i]=new Integer(arr[i]);
}
Arrays.sort(a,new Comparator<Integer>(){
public int compare(Integer a,Integer b){
return Integer.bitCount(a)==Integer.bitCount(b)?a-b:Integer.bitCount(a)-Integer.bitCount(b);
}
});
return arr;
}
结果错误???不知道原因??????????
java比较器
比较器概述:
java主要有两种比较器,非别为Comparable与Comparator两种
1、Comparable为内部比较器,为一个接口,接口中只有compareTo()一个方法。只能接收引用类型
public interface Comparable<T> {
public int compareTo(T o);
}
需要排序的对象费实现Comparable接口的compareTo方法。对其进行了向下转型
private static void binarySort(Object[] a, int lo, int hi, int start) {
Comparable pivot = (Comparable) a[start];
...
// Set left (and right) to the index where a[start] (pivot) belongs
int left = lo;
int right = start;
assert left <= right;
while (left < right) {
int mid = (left + right) >>> 1;
if (pivot.compareTo(a[mid]) < 0) //重写的compareTo方法,为Comparable对象的方法
right = mid;
else
left = mid + 1;
}
...
}
2、Comparator为外部比较器。也是一个接口。只能接收引用类型
public interface Comparator<T> {
int compare(T o1, T o2);
boolean equals(Object obj);
}
注意:有些类实现了comparator,但是没有实现equals方式是因为Object类已经实现了equals方法
在接口中重写compare()方法,在Arrays.sort()的过程中,创建了匿名类,进而调用复写的compare方法
private static <T> void binarySort(T[] a, int lo, int hi, int start,
Comparator<? super T> c) {
...
for ( ; start < hi; start++) {
T pivot = a[start];
// Set left (and right) to the index where a[start] (pivot) belongs
int left = lo;
int right = start;
assert left <= right;
pivot < all in [right, start).
while (left < right) {
int mid = (left + right) >>> 1;
if (c.compare(pivot, a[mid]) < 0) //重写的compare方法
right = mid;
else
left = mid + 1;
}
...
比较器的使用
1、comparable:
首先,定义一个Person实体类实现Comparable接口
public class Person implements Comparable<Person>{
public int age; //年龄
public String name; //姓名
//get、set方法
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Person(int age, String name) {
super();
this.age = age;
this.name = name;
}
@Override
public String toString() {
return "Person [age=" + age + ", name=" + name + "]";
}
//实现Comparable接口必须实现compareTo方法
public int compareTo(Person o) {
if(this.age==o.age&&this.name==o.name){
return 0;
}else if(this.age>o.age){
System.out.println("this.age:"+this.age+"o.age:"+o.age);
return 1;
}else{
return -1;
}
}
public Person() {
super();
// TODO Auto-generated constructor stub
}
}
接着,编写测试类进行测试。
import org.junit.Test;
import junit.framework.TestCase;
public class TestCompare extends TestCase {
@Test
public void test1(){
List<Person> list=new ArrayList<Person>();
Person test1=new Person(66,"李四");
Person test2=new Person(29,"王五");
Person test3=new Person(28,"赵六");
Person test4=new Person(20,"钱三");
list.add(test4);
list.add(test3);
list.add(test2);
list.add(test1);
Collections.sort(list);
for(Object s:list){
System.out.println(s);
}
}
}
运行结果:对象实现了comparable接口就自动排序
this.age:28o.age:20
this.age:29o.age:28
this.age:66o.age:29
Person [age=20, name=钱三]
Person [age=28, name=赵六]
Person [age=29, name=王五]
Person [age=66, name=李四]
2、Comparator
定义一个实体类
public class emp {
public int age;
public String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public emp(int age, String name) {
super();
this.age = age;
this.name = name;
}
public emp() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "emp [age=" + age + ", name=" + name + "]";
}
}
编写测试类测试
import org.junit.Test;
import junit.framework.TestCase;
public class TestCompare extends TestCase {
@Test
public void test2(){
List<emp> list=new ArrayList<emp>();
emp test1=new emp(69,"李四");
emp test2=new emp(29,"王五");
emp test3=new emp(28,"赵六");
emp test4=new emp(20,"钱三");
list.add(test4);
list.add(test3);
list.add(test2);
list.add(test1);
Collections.sort(list,new Comparator<emp>(){
@Override
public int compare(emp o1, emp o2) {
if(o1.age==o2.age&&o1.name==o2.name){
return 0;
}else if(o1.age>o2.age){
return 1;
}else{
return 0;
}
}
});
for(Object s:list){
System.out.println(s);
}
}
}
运行结果:
emp [age=20, name=钱三]
emp [age=28, name=赵六]
emp [age=29, name=王五]
emp [age=69, name=李四]
与2020/10/13完成
参考链接:
上一篇: 集合不安全