//数组
public static void main(String[] args) {
//1. 数组排序和查找
{
int[] intA = {5, 4, 2, 3, 1};
String[] StrA = {"ab", "cd", "ef"}; Arrays.sort(intA); for(int a : intA){
System.out.println("1 :"+a);
}
//采用的是二分法查找,所以必须要先排序
int i = Arrays.binarySearch(intA, 3);
System.out.println("1 :索引是从0开始,索引为"+i);
}
//2. 获取数组长度
{
int[] intA = {5, 4, 2, 3, 1};
int[][] intB = new int[5][4]; System.out.println("2 :"+intA.length);
System.out.println("2 :"+intB.length+", "+intB[0].length);
}
//3. 数组反转(也可以仍为是集合的反转)
{
ArrayList<String> al = new ArrayList<String>(); al.add("a");
al.add("b");
al.add("c");
al.add("d");
//采用集合的静态工具Collections进行反转
Collections.reverse(al);
Iterator it = al.iterator();
while(it.hasNext()){
System.out.println("3 :"+it.next());
}
}
//4. 找到数组的最大值和最小值
{
Integer[] intA = {5, 4, 2, 3, 1};
//数组转换成集合里的数组List
//注意:这里的ArrayList并非java.util.ArrayList,而是Arrays类的静态内部类!
Integer intB = Collections.max(Arrays.asList(intA));
Integer intC = Collections.min(Arrays.asList(intA));
System.out.println("4 :"+intB+", "+intC);
}
//5. 数组合并
{
Integer[] intA = {5, 4, 2, 3, 1};
Integer[] intB = {8, 9, 7, 6};
//现将数组intA转换为内部集合,然后在放入ArrayList中
ArrayList al = new ArrayList(Arrays.asList(intA));
al.addAll(Arrays.asList(intB)); //第一种输出方式
Iterator it = al.iterator();
while(it.hasNext()){
System.out.println("5 :"+it.next());
}
//第二种输出方式
System.out.println("5 :"+al);
}
//6. 填充数组元素
{
int[] intA = new int[3];
Arrays.fill(intA, 1);
for(int a : intA)
System.out.println("6 :"+a); System.out.println("6 --------");
Arrays.fill(intA, 1, 3, 2);
for(int a : intA)
System.out.println("6 :"+a); System.out.println("6 --------");
Integer[] intB = new Integer[3];
Arrays.fill(intB, 100);
for(Integer a : intB)
System.out.println("6 :"+a); }
//7. 数组扩容
{
int[] intA = {5, 4, 2, 3, 1};
int[] intB = new int[7];
intB[5] = 5;
intB[6] = 6;
System.arraycopy(intA, 0, intB, 0, intA.length);
for(int a : intB)
System.out.println("7 :"+a);
}
//8. 删除元素
{
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(10);
al.add(20);
al.add(30);
al.remove(1);
System.out.println("8 :"+al);
}
//9. 查看数组中是否包含某个元素
{
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(10);
al.add(20);
al.add(30);
System.out.println("9 :"+al.contains(20));
System.out.println("9 :"+al.contains(40));
}
//10. 查看数组是否相等
{
int[] intA = {1,2,3,4,5};
int[] intB = {1,2,3,4};
int[] intC = {1,2,3,4,5};
Integer[] intD = {1,2,3,4,5}; System.out.println("10 :"+Arrays.equals(intA, intB));
System.out.println("10 :"+Arrays.equals(intA, intC));
//System.out.println("10 :"+Arrays.equals(intA, intD));//报错
}
}

结果:

1  :1
1 :2
1 :3
1 :4
1 :5
1 :索引是从0开始,索引为2
2 :5
2 :5, 4
3 :d
3 :c
3 :b
3 :a
4 :5, 1
5 :5
5 :4
5 :2
5 :3
5 :1
5 :8
5 :9
5 :7
5 :6
5 :[5, 4, 2, 3, 1, 8, 9, 7, 6]
6 :1
6 :1
6 :1
6 --------
6 :1
6 :2
6 :2
6 --------
6 :100
6 :100
6 :100
7 :5
7 :4
7 :2
7 :3
7 :1
7 :5
7 :6
8 :[10, 30]
9 :true
9 :false
10 :false
10 :true

最新文章

  1. 面试题目——《CC150》位操作
  2. linux下安装安装pcre-8.32
  3. Java算法-冒泡排序
  4. C#把datetime类型的日期转化成年月日或其他格式方法总结
  5. js计算在线时长
  6. java笔记之变量的存储方式
  7. Data Flow -&gt;&gt; CDC Control Task, CDC Source, CDC Splitter
  8. 项目中使用Quartz集群分享--转载
  9. hdoj 1106 排序
  10. QML学习心得
  11. zoj 2972 - Hurdles of 110m
  12. 微信小程序(有始有终,全部代码)开发--- 新增模块: 图片选取以及拍照功能
  13. GoogleNet:inceptionV3论文学习
  14. js 数组去重常见的几种方式
  15. [ SSH框架 ] Struts2框架学习之一
  16. Spring-data-redis操作redis知识总结
  17. POJ 1966 Cable TV Network (算竞进阶习题)
  18. Window通过Web方式修改域用户密码
  19. SSM框架搭建教程(从零开始,图文结合)
  20. 华为核心交换机绑定IP+MAC+端口案例

热门文章

  1. 流与文本文件操作(C#)
  2. UWP开发入门(一)——SplitView
  3. Syncthing源码解析 - 源码目录说明!
  4. BZOJ3210: 花神的浇花集会(坐标系变换)
  5. Getting Started with Elastic Search in .NET
  6. jquery源码解析:jQuery数据缓存机制详解2
  7. Settings app简单学习记录
  8. elment 中 el-table 进行校验
  9. [Alpha]团队任务拆解
  10. python全栈开发_day13_迭代器和生成器