# day17将数组变成list集合;自定义排序，必须复写比较器;集合变数组

76人阅读 评论(0)

/*
Arrays：里面全是操作数组的工具类

asList：将数组变成list集合
*/
import java.util.*;
class  ArraysDemo
{
public static void main(String[] args)
{
//int[] arr={2,4,5};
//System.out.println(Arrays.toString(arr));

//String[] arr={"abc","cc","kkk"};

//把数组变成list组合有什么好处
/*

*/
List<String> list=Arrays.asList(arr);
//sop("contains:"+list.contains("cc"));

//sop(list);
int[] nums={2,4,5};
Integer[] nums={2,4,5};
List<int[]> li=Arrays.asList(nums);
/*

*/
sop(list);
}
public static void sop(Object obj)
{
System.out.println(obj);
}

}

------------------------------------

import java.util.*;
class CollectionsDemo
{
public static void main(String[] args)
{
shuffleDemo();
}
public static void binarySearchDemo()
{
List<String> list=new ArrayList<String>();

Collections.sort(list,new StrLenComparator());
sop(list);

//int index=Collections.binarySearch(list,"aaaa");//默认的二分查找
//int index=halfSearch(list,"aaaa");
int index=halfSearch2(list,"aaa",new StrLenComparator());//复写的二分查找，并返回值
sop("index="+index);
}

public static int halfSearch(List<String> list,String key)
{
int max,min,mid;
max=list.size()-1;
min=0;

while (min<max)
{
mid=(max+min)>>1;
String str=list.get(mid);
int num=str.compareTo(key);
if (num>0)
max=mid-1;
else if(num<0)
min=mid+1;
else
return mid;
}
return -min-1;
}
public static int halfSearch2(List<String> list,String key,Comparator<String> cmp)
{
int max,min,mid;
max=list.size()-1;
min=0;
while (min<max)
{
mid=(max+min)>>1;
String str=list.get(mid);
int num=cmp.compare(str,key);
if (num>0)
max=mid-1;
else if(num<0)
min=mid+1;
else
return mid;
}
return -min-1;
}

public static void MaxDemo()
{
List<String> list=new ArrayList<String>();

Collections.sort(list);
sop(list);
String max=Collections.max(list,new StrLenComparator());
sop("max:"+max);
}

public static void shuffleDemo()//随机排列（调用一次，朴克牌洗一次）
{
List<String> list=new ArrayList<String>();

sop(list);
Collections.shuffle(list);
sop(list);
}
public static void SortDemo()
{
List<String> list=new ArrayList<String>();

sop(list);

//Collections.sort(list);//自然规则排序
Collections.sort(list,new StrLenComparator());//自定义排序，必须复写比较器

sop(list);
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
class StrLenComparator implements Comparator<String>
{
public int compare(String s1,String s2)
{
if (s1.length()>s2.length())
return 1;
if (s1.length()<s2.length())
return -1;
return s1.compareTo(s2);
}
}

-----------------------------------

import java.util.*;
class StrComparator implements Comparator<String>
{
public int compare(String s1,String s2)
{
return s2.compareTo(s1);
}
}
class StrLenComparator implements Comparator<String>
{
public int compare(String s1,String s2)
{
if (s1.length()>s2.length())
return 1;
if (s1.length()<s2.length())
return -1;
return s1.compareTo(s2);
}
}
class  CollectionsDemo2
{
public static void main(String[] args)
{
orderDemo();
}
public static void orderDemo()
{
TreeSet<String> ts=new TreeSet<String>(Collections.reverseOrder(new StrLenComparator()));
Iterator it=ts.iterator();
while (it.hasNext())
{
sop(it.next());
}

}
public static void replaceAllDemo()
{
List<String> list=new ArrayList<String>();

sop(list);
Collections.replaceAll(list,"aaa","pp");
sop(list);
Collections.reverse(list);
sop(list);
}
/*

*/
public static void fillDemo()
{
List<String> list=new ArrayList<String>();

sop(list);
Collections.fill(list,"pp");
sop(list);
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}

--------------------------------------------

/*

Collection接口中的toArray方法
*/
import java.util.*;
class  CollectionToArray
{
public static void main(String[] args)
{
ArrayList<String> al=new ArrayList<String>();

/*
1、指定类型的数组到底定义多长呢

2、为什么要将集合变数组

*/
String[] arr=al.toArray(new String[al.size()]);
sop(Arrays.toString(arr));
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：4648次
• 积分：495
• 等级：
• 排名：千里之外
• 原创：9篇
• 转载：47篇
• 译文：30篇
• 评论：0条
文章分类
文章存档
阅读排行