前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Arrays 类及基本使用

Arrays 类及基本使用

作者头像
Cell
发布2022-02-25 16:13:58
1750
发布2022-02-25 16:13:58
举报
文章被收录于专栏:Cell的前端专栏

主要方法

  • static type[] copyof(type[] original,int length)
  • static int binarysearch(type[] a,type key)
  • static boolean equals(type[] a,type[] b)
  • static void fill(type[] a,type val)
  • static void fill(type[] a,int fromindex,int toindex,type val)
  • static void sort(type[] a)

实例代码

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

package Arrays; import java.text.Collator; import java.util.Arrays; import java.util.Comparator; public class ArraysDemo { public static void main(String agrs[]) { Integer arr[]=new Integer[9]; for(int i=0;i<9;i++) arr[i]=(int)(Math.random()*100); //显示,排序数组 System.out.print("原内容:"); display(arr); Arrays.sort(arr); System.out.print("排序后:"); display(arr); //将值-1 分配给数组 arr 中下标从 0 到 3-1 的位置 Arrays.fill(arr, 0,3,-1); System.out.print("fill() 后:"); display(arr); //搜索 23 System.out.print("值 23 的位置:"); int index =Arrays.binarySearch(arr, 23);//二分查找 System.out.print(index);//如果查找不到,index 为负 System.out.print("\n 插入 0 在 3 号位置:"); Arrays.fill(arr,3,4,0); display(arr); System.out.print("值 0 的位置:"); index =Arrays.binarySearch(arr, 0); System.out.print(index); Integer arr2[]=new Integer[8]; arr2=Arrays.copyOf(arr, arr2.length); //复制 8 个 System.out.print("\n 复制后的数组:"); display(arr2); if(Arrays.equals(arr, arr2)) System.out.println("两数组相同!"); else System.out.println("两数组不相同!"); System.out.println("----------------------------------------"); String[] str = {"计算机","黄桑","通信","李瑞豪"}; Arrays.sort(str); for(int i=0;i<str.length;i++) System.out.print(str[i]+" "); System.out.println(""); //Collator 类是用来执行分语言环境的字符串比较,这里用的 CHINA Comparator com=Collator.getInstance(java.util.Locale.CHINA);//获取 Comparator 对象,参数表示按中文排序 //根据指定的 "比较器" 产生的顺序对 "指定对象数组" 进行排序 Arrays.sort(str,com);//sort(T[] a,Comparator<?super T>c) for(int i=0;i<str.length;i++) System.out.print(str[i]+" "); } static void display(Integer arr[]) { for(int i=0;i<arr.length;i++) System.out.print(arr[i]+" "); System.out.println(""); } }

程序运行结果

1 2 3 4 5 6 7 8 9 10 11

原内容:41 0 44 96 49 96 30 6 87 排序后:0 6 30 41 44 49 87 96 96 fill() 后:-1 -1 -1 41 44 49 87 96 96 值 23 的位置:-4 插入 0 在 3 号位置:-1 -1 -1 0 44 49 87 96 96 值 0 的位置:3 复制后的数组:-1 -1 -1 0 44 49 87 96 两数组不相同! ---------------------------------------- 李瑞豪 计算机 通信 黄桑 黄桑 计算机 李瑞豪 通信

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-01-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 主要方法
  • 实例代码
  • 程序运行结果
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档