我们讲到了一维数组和二维数组以及开发工具eclipse的配置
java.util.Arrays 类能方便地操作数组,它提供的所有方法都是静态的。
具有以下功能:
Arrays类是 Java中用来操作数组 的模块他的使用方法是在Java类中使用 import java.util.Arrays 进行导入,并使用 Arrays.方法() 进行调用方法!
fill方法有两种用途!
第一种就是填充数组,将数组中的全部元素转换为所输入的元素
第二种就是替换数组元素,将数组中某个元素进行单个替换
在初始化一个数组之后,如果没有给数组的元素赋值,那么这个数组中的 元素默认是为0 的,那么我们 一个个进行赋值又会略显麻烦,会堆积代码 !所以我们就需要用到 fill方法进行填充, 但是这么做 会让全部元素变成同一个数值!
class="prettyprint hljs java" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">import java.util.Arrays; //这里导入Arrays类
- public class Fill{
- public static void main(String[] args){
- int[] mylist = new int[5]; //这里创建一个名称为mylist的数组,数组的元素个个数为5
- Arrays.fill(mylist,3); //为数组填充3,格式为fill(列表,数值)
- for(int x:mylist){
- System.out.println(x);
- } //通过for each来遍历数组元素
- }
- }
在给元素 赋值完或者是填充完元素 之后,如果想 对某个元素进行修改 ,那么我们就要 重新赋值或者是替换元素 ,但是 重新赋值会增加代码 ,让代码显得更繁琐,所以 Arrays类中提供了替换元素的方法fill!
"prettyprint hljs java" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">import java.util.Arrays;
- public class Fill{
- public static void main(String[] args){
- int[] mylist = {1,2,3,4};
- Arrays.fill(mylist, 1,2,4);
- for(int x:mylist){
- System.out.println(x);
- }
-
- } //这是一个特殊的格式Arrays.fill(列表名称,空格正向索引,反向索引,改变的数值)
-
- }

这里的正反向索引指向的一定要是同一个元素!
在Java程序的使用过程中,有时候会需要一个 含有相同或者是部分相同元素的数组 ,但是 重新创建数组的话就会增加代码长度,减少代码可读性 ,那么我们就可以使用到 复制数组或者是部分数组的方法!
☄️copyOf方法提供了 多种重载的方法 , 用以复制数组,增加代码可读性 。该方法 不受数组长度的限制,若超出,则多处部分为0!
class="prettyprint hljs cs" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">import java.util.Arrays;
- public class Fill{
- public static void main(String[] args){
- int[] mylist = {1,2,3,4};
- int[] justlist = Arrays.copyOf(mylist,4); //将复制后的数组赋值给justlist
- //格式Arrays.copyOf(列表,复制后的长度)
- for(int x:justlist){
- System.out.println(x);
- }
- System.out.println(mylist);
- System.out.println(justlist); //这里输出两个数组的内存空间进行检查
- }
-
- //这是一个特殊的格式Arrays.fill(列表名称,空格正向索引,反向索引,改变的数值)
-
- }
解:从以上结果可以看出 赋值成功了,并且内存空间不同 ( 下面我会解释为什么要输出内存空间 )
有时候在 编辑代码的时候只需要中间一部分代码 ,但是 copyOf方法只能复制以前面部分为开头的元素,而不能直接复制中间的代码 ,为了解决这一个问题,这个类提供了 另一个方法copyOfRange方法(中文意思:选择复制)利用这个方法就可以解决这一个问题 !
"prettyprint hljs java" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">import java.util.Arrays;
- public class Fill{
- public static void main(String[] args){
- int[] mylist = {1,2,3,4};
- int[] justlist = Arrays.copyOfRange(mylist,1,3);
- //Arrays类的方法使用形式Arrays.copyOfRange(列表,第一个索引位置,第二个索引位置)
- for(int x:justlist){
- System.out.println(x);
- }
- }
- }

注:在末尾有问题解答!
在代码编译过程中,有时候会需要用到 有序的一组数组才能进行更好的操作 ,但是我们重新进行编译会增加代码数量,所以我们要对代码进行排序, Java中提供了sort方法对数组的元素进行升序排序!
在Java编译过程中, 有顺序的数组会让你的编译更加方便,使得你自己以及其他参与编译的人更加清楚,尤其是适合那些大基数的数组更为适用和实用 !
"prettyprint hljs java" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">import java.util.Arrays;
- public class Fill{
- public static void main(String[] args){
-
- int[] mylist = {1,7,33,4};
- Arrays.sort(mylist);
- //方式为Arrays.sort(列表)
- for(int x:mylist){
- System.out.println(x);
- }
- }
- }

问题解答