- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章深入了解Java核心类库--Arrays类由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
类Arrays包含用于操作数组的各种方法(例如排序和搜索) 。
返回指定数组内容的字符串形式 。
举例 。
1
2
|
int
[] a1 = {
1
,
2
,
3
,
4
,
5
};
System.out.println(Arrays.toString(a1));
//[1, 2, 3, 4, 5]
|
源码 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public
static
String toString(
int
[] a) {
if
(a ==
null
)
return
"null"
;
int
iMax = a.length -
1
;
if
(iMax == -
1
)
return
"[]"
;
StringBuilder b =
new
StringBuilder();
b.append(
'['
);
for
(
int
i =
0
; ; i++) {
b.append(a[i]);
if
(i == iMax)
return
b.append(
']'
).toString();
b.append(
", "
);
}
}
|
其它 。
Modifier and Type | Field | Description |
---|---|---|
static String | deepToString(Object[] a) | 返回指定数组的“深层内容”的字符串表示形式 |
排序(默认升序) 。
指定区间进行排序 。
举例 。
1
2
3
4
|
int
[] a1 = {
9
,
1
,
3
,
7
,
2
,
5
};
System.out.println(Arrays.toString(a1));
//[9, 1, 3, 7, 2, 5]
Arrays.sort(a1,
0
,
3
);
//[0,3),对9,1,3进行排序
System.out.println(Arrays.toString(a1));
//[0[1, 3, 9, 7, 2, 5]
|
源码 。
1
2
3
4
|
public
static
void
sort(
int
[] a,
int
fromIndex,
int
toIndex) {
rangeCheck(a.length, fromIndex, toIndex);
DualPivotQuicksort.sort(a, fromIndex, toIndex -
1
,
null
,
0
,
0
);
}
|
对整个数组进行排序 。
举例 。
1
2
3
4
|
int
[] a1 = {
0
,
7
,
8
,
2
,
4
,
1
};
System.out.println(Arrays.toString(a1));
//[0, 7, 8, 2, 4, 1]
Arrays.sort(a1);
System.out.println(Arrays.toString(a1));
//[0, 1, 2, 4, 7, 8]
|
源码 。
1
2
|
public
static
void
sort(
int
[] a) { DualPivotQuicksort.sort(a,
0
, a.length -
1
,
null
,
0
,
0
);
}
|
Modifier and Type | Field | Description |
---|---|---|
static void | sort(T[] a, int fromIndex, int toIndex, Comparator<? super T> c) | 根据指定比较器引发的顺序对指定对象数组的指定范围进行排序。 |
static void | sort(T[] a, Comparator<? super T> c) | 根据指定比较器引发的顺序对指定的对象数组进行排序。 |
static void | parallelSort(T[] a) | 将指定的数组按升序排序。 |
static void | parallelSort(T[] a, int fromIndex, int toIndex) | 将指定的数组范围按数字升序排序。 |
static <T extends Comparable<? super T>>void | parallelSort(T[] a) | 根据元素的natural ordering对指定的对象数组按升序排序。 |
static <T extends Comparable<? super T>>void | parallelSort(T[] a, int fromIndex, int toIndex) | 根据元素的natural ordering ,将指定对象数组的指定范围按升序排序。 |
static void | parallelSort(T[] a, int fromIndex, int toIndex, Comparator<? super T> cmp) | 根据指定比较器引发的顺序对指定对象数组的指定范围进行排序。 |
static void | parallelSort(T[] a, Comparator<? super T> cmp) | 根据指定比较器引发的顺序对指定的对象数组进行排序。 |
复制(常用于数组扩容) 。
举例 。
1
2
3
4
|
int
[] a = {
1
,
2
,
3
};
System.out.println(a.length);
//output:3
a = Arrays.copyOf(a,
15
);
System.out.println(a.length);
//output:15
|
源码 。
1
2
3
4
5
|
public
static
int
[] copyOf(
int
[] original,
int
newLength) {
int
[] copy =
new
int
[newLength];
System.arraycopy(original,
0
, copy,
0
, Math.min(original.length, newLength));
return
copy;
}
|
其它 。
Modifier and Type | Field | Description |
---|---|---|
static T[] | copyOf(T[] original, int newLength) | 使用空值复制指定的数组,截断或填充(如有必要),以使副本具有指定的长度 |
static <T,U>T[] | copyOf(U[] original, int newLength, 类<? extends T[]> newType) | 使用空值复制指定的数组,截断或填充(如有必要),以使副本具有指定的长度 |
举例 。
1
2
3
4
5
|
int
[] a1 = {
0
,
1
,
2
,
3
,
4
,
5
};
int
[] a2 = {
0
,
1
,
2
,
3
,
4
,
5
};
//与a1相同
int
[] a3 = {
0
,
1
,
2
,
3
,
0
,
5
};
//从索引4开始与a1不同
System.out.println(Arrays.mismatch(a1,a2));
//output:-1
System.out.println(Arrays.mismatch(a1,a3));
//output:4
|
源码 。
1
2
3
4
5
6
7
|
public
static
int
mismatch(
int
[] a,
int
[] b) {
int
length = Math.min(a.length, b.length);
// Check null array refs
if
(a == b)
return
-
1
;
int
i = ArraysSupport.mismatch(a, b, length);
return
(i <
0
&& a.length != b.length) ? length : i;
}
|
其它 。
Modifier and Type | Field | Description |
---|---|---|
static int | mismatch(T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex, Comparator<? super T> cmp) | 查找并返回指定范围内两个 Object数组之间第一个不匹配的相对索引,否则如果未找到不匹配则返回-1。 |
static int | mismatch(T[] a, T[] b, Comparator<? super T> cmp) | 查找并返回两个 Object数组之间第一个不匹配的索引,否则如果未找到不匹配则返回-1。 |
二分查找,搜索,返回下标 。
限定了搜索的范围[fromIndex, toIndex) 。
举例 。
1
2
3
|
int
[] a = {
1
,
2
,
3
,
4
,
5
};
int
x1 = Arrays.binarySearch(a,
2
,
3
,
4
);
//在a数组下标[2,3)中查找值为4的下标
System.out.println(x1);
//output:<0的随机数
|
源码 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
public
static
int
binarySearch(
int
[] a,
int
fromIndex,
int
toIndex,
int
key) {
rangeCheck(a.length, fromIndex, toIndex);
return
binarySearch0(a, fromIndex, toIndex, key);
}
private
static
int
binarySearch0(
int
[] a,
int
fromIndex,
int
toIndex,
int
key) {
int
low = fromIndex;
int
high = toIndex -
1
;
while
(low <= high) {
int
mid = (low + high) >>>
1
;
int
midVal = a[mid];
if
(midVal < key)
low = mid +
1
;
else
if
(midVal > key)
high = mid -
1
;
else
return
mid;
// key found
}
return
-(low +
1
);
// key not found.
}
|
与上述相同,只是没有限定范围,fromIndex=0, toIndex=length 。
举例 。
1
2
3
4
5
|
int
[] a = {
1
,
2
,
3
,
4
,
5
};
int
x1 = Arrays.binarySearch(a,
3
);
//在a数组中查找值为3的下标
int
x2 = Arrays.binarySearch(a,-
6
);
//在a数组中查找值为6的下标
System.out.println(x1);
//output:2
System.out.println(x2);
//output:<0的随机数
|
源码 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public
static
int
binarySearch(
int
[] a,
int
key) {
return
binarySearch0(a,
0
, a.length, key);
}
private
static
int
binarySearch0(
int
[] a,
int
fromIndex,
int
toIndex,
int
key) {
int
low = fromIndex;
int
high = toIndex -
1
;
while
(low <= high) {
int
mid = (low + high) >>>
1
;
int
midVal = a[mid];
if
(midVal < key)
low = mid +
1
;
else
if
(midVal > key)
high = mid -
1
;
else
return
mid;
// key found
}
return
-(low +
1
);
// key not found.
}
|
Modifier and Type | Field | Description |
---|---|---|
static int | binarySearch(T[] a, int fromIndex, int toIndex, T key, Comparator<? super T> c) | 使用二进制搜索算法搜索指定对象的指定数组范围 |
static int | binarySearch(T[] a, T key, Comparator<? super T> c) | 使用二进制搜索算法在指定的数组中搜索指定的对象 |
如果两个指定数组在指定范围内相等 ,则返回true 。
举例 。
1
2
3
4
|
int
[] a1 = {
1
,
2
,
3
,
4
,
5
};
int
[] a2 = {
1
,
2
,
0
,
0
,
4
,
5
};
System.out.println(Arrays.equals(a1,
0
,
2
,a2,
0
,
2
));
//true
System.out.println(Arrays.equals(a1,
3
,
5
,a2,
4
,
6
));
//true
|
源码 。
1
2
3
4
5
6
7
8
9
|
public
static
boolean
equals(
int
[] a,
int
aFromIndex,
int
aToIndex,
int
[] b,
int
bFromIndex,
int
bToIndex) {
rangeCheck(a.length, aFromIndex, aToIndex);
rangeCheck(b.length, bFromIndex, bToIndex);
int
aLength = aToIndex - aFromIndex;
int
bLength = bToIndex - bFromIndex;
if
(aLength != bLength)
return
false
;
return
ArraysSupport.mismatch(a, aFromIndex, b, bFromIndex,aLength) <
0
;
}
|
如果两个指定数组相等,则返回 true 。
举例 。
1
2
3
4
5
|
int
[] a1 = {
1
,
2
,
3
,
4
,
5
};
int
[] a2 = {
1
,
2
,
3
,
4
,
5
};
int
[] a3 = {
1
,
2
,
0
,
4
,
5
};
System.out.println(Arrays.equals(a1,a2));
//true
System.out.println(Arrays.equals(a1,a3));
//false
|
源码 。
1
2
3
4
5
6
7
8
9
10
|
public
static
boolean
equals(
int
[] a,
int
[] a2) {
if
(a==a2)
return
true
;
if
(a==
null
|| a2==
null
)
return
false
;
int
length = a.length;
if
(a2.length != length)
return
false
;
return
ArraysSupport.mismatch(a, a2, length) <
0
;
}
|
Modifier and Type | Field | Description |
---|---|---|
static boolean | deepEquals(Object[] a1, Object[] a2) | 如果两个指定的数组彼此 深度相等 ,则返回 true |
static boolean | equals(T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex, Comparator<? super T> cmp) | 如果在指定范围内指定的两个Object数组彼此 相等 ,则返回true |
static boolean | equals(T[] a, T[] a2, Comparator<? super T> cmp) | 如果两个指定的Objects数组彼此 相等 ,则返回 true |
将指定的T值分配给指定的T类型数组的指定范围的每个元素 。
举例 。
1
2
3
4
5
6
|
int
[] a1 =
new
int
[
10
];
Arrays.fill(a1,
1
,
4
,
8
);
char
[] a2 =
new
char
[
10
];
Arrays.fill(a2,
0
,
3
,
's'
);
System.out.println(Arrays.toString(a1));
//[0, 8, 8, 8, 0, 0, 0, 0, 0, 0]
System.out.println(Arrays.toString(a2));
//[s, s, s, , , , , , , ]
|
源码 。
1
2
3
4
5
|
public
static
void
fill(
char
[] a,
int
fromIndex,
int
toIndex,
char
val) {
rangeCheck(a.length, fromIndex, toIndex);
for
(
int
i = fromIndex; i < toIndex; i++)
a[i] = val;
}
|
将指定的T值分配给指定的T类型数组的每个元素 。
举例 。
1
2
3
4
5
6
|
int
[] a1 =
new
int
[
10
];
Arrays.fill(a1,
8
);
char
[] a2 =
new
char
[
10
];
Arrays.fill(a2,
's'
);
System.out.println(Arrays.toString(a1));
//[8, 8, 8, 8, 8, 8, 8, 8, 8, 8]
System.out.println(Arrays.toString(a2));
//[s, s, s, s, s, s, s, s, s, s]
|
源码 。
1
2
3
4
|
public
static
void
fill(
int
[] a,
int
val) {
for
(
int
i =
0
, len = a.length; i < len; i++)
a[i] = val;
}
|
Modifier and Type | Field | Description |
---|---|---|
static List | asList(T… a) | 返回由指定数组支持的固定大小的列表。 |
static int | compare(T[] a, T[] b) | 字典顺序比较两个T阵列 |
static int | compare(T[] a, int aFromIndex, int aToIndex,T[] b, int bFromIndex, int bToIndex) | 在指定范围内按字典顺序比较两个T阵列 |
static <T extends Comparable<? super T>>int | compare(T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex) | 在指定范围内按字典顺序比较两个 Object阵列。 |
static int | compare(T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex, Comparator<? super T> cmp) | 在指定范围内按字典顺序比较两个 Object阵列。 |
static <T extends Comparable<? super T>>int | compare(T[] a, T[] b) | 按 Object顺序比较两个 Object阵列,在可比元素中。 |
static int | compare(T[] a, T[] b, Comparator<? super T> cmp) | 使用指定的比较器按字典顺序比较两个 Object阵列 |
static int | compareUnsigned(T[] a, T[] b) | byte字典顺序比较两个T阵列,数字处理元素为无符号 |
static int | compareUnsigned(T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex) | 在指定范围内按字典顺序比较两个 T阵列,将元素数字处理为无符号 |
static T[] | copyOfRange(T[] original, int from, int to) | 将指定数组的指定范围复制到新数组中 |
static T[] | copyOfRange(T[] original, int from, int to) | 将指定数组的指定范围复制到新数组中 |
static <T,U>T[] | copyOfRange(U[] original, int from, int to, 类<? extends T[]> newType) | 将指定数组的指定范围复制到新数组中 |
static int | hashCode(T[] a) | 根据指定数组的内容返回哈希码 |
static int | deepHashCode(Object[] a) | 返回基于指定数组的“深层内容”的哈希码 |
static void | parallelPrefix(T[] array, int fromIndex, int toIndex,TBinaryOperator op) | 对于给定的数组子范围执行parallelPrefix(T[], TBinaryOperator) |
static void | parallelPrefix(dT[] array, TBinaryOperator op) | 使用提供的函数并行地累积给定数组的每个元素 |
static void | parallelPrefix(T[] array, int fromIndex, int toIndex, BinaryOperator op) | 对于给定的数组子范围执行 parallelPrefix(Object[], BinaryOperator) |
static void | parallelPrefix(T[] array, BinaryOperator op) | 使用提供的函数并行地累积给定数组的每个元素 |
static void | parallelSetAll(double[] array, IntToDoubleFunction generator) | 使用提供的生成器函数并行设置指定数组的所有元素以计算每个元素 |
static void | parallelSetAll(int[] array, IntUnaryOperator generator) | 使用提供的生成器函数并行设置指定数组的所有元素以计算每个元素 |
static void | parallelSetAll(long[] array, IntToLongFunction generator) | 使用提供的生成器函数并行设置指定数组的所有元素以计算每个元素 |
static void | parallelSetAll(T[] array, IntFunction<? extends T> generator) | 使用提供的生成器函数并行设置指定数组的所有元素以计算每个元素 |
static void | setAll(double[] array, IntToDoubleFunction generator) | 使用提供的生成器函数设置指定数组的所有元素以计算每个元素 |
static void | setAll(int[] array, IntUnaryOperator generator) | 使用提供的生成器函数设置指定数组的所有元素以计算每个元素 |
static void | setAll(long[] array, IntToLongFunction generator) | 使用提供的生成器函数设置指定数组的所有元素以计算每个元素 |
static void | setAll(T[] array, IntFunction<? extends T> generator) | 使用提供的生成器函数设置指定数组的所有元素以计算每个元素 |
static | Spliterator.OfDouble spliterator(double[] array) | 返回覆盖所有指定数组的Spliterator.OfDouble |
static | Spliterator.OfDouble spliterator(double[] array, int startInclusive, int endExclusive) | 返回覆盖指定数组的指定范围的Spliterator.OfDouble |
static | Spliterator.OfInt spliterator(int[] array) | 返回覆盖所有指定数组的Spliterator.OfInt |
static | Spliterator.OfInt spliterator(int[] array, int startInclusive, int endExclusive) | 返回覆盖指定数组的指定范围的Spliterator.OfInt |
static | Spliterator.OfLong spliterator(long[] array) | 返回覆盖所有指定数组的Spliterator.OfLong |
static | Spliterator.OfLong spliterator(long[] array, int startInclusive, int endExclusive) | 返回覆盖指定数组的指定范围的Spliterator.OfLong |
static Spliterator | spliterator(T[] array) | 返回覆盖所有指定数组的Spliterator |
static Spliterator | spliterator(T[] array, int startInclusive, int endExclusive) | 返回覆盖指定数组的指定范围的Spliterator |
static DoubleStream | stream(double[] array) | 返回以指定数组作为源的顺序DoubleStream |
static DoubleStream | stream(double[] array, int startInclusive, int endExclusive) | 返回指定数组的指定范围作为其源的顺序DoubleStream |
static IntStream | stream(int[] array) | 返回以指定数组作为源的顺序IntStream |
static IntStream | stream(int[] array, int startInclusive, int endExclusive) | 返回指定数组的指定范围作为其源的顺序IntStream |
static LongStream | stream(long[] array) | 返回以指定数组作为源的顺序LongStream |
static LongStream | stream(long[] array, int startInclusive, int endExclusive) | 返回指定数组的指定范围作为其源的顺序LongStream |
static Stream | stream(T[] array) | 返回以指定数组作为源的顺序Stream |
static Stream | stream(T[] array, int startInclusive, int endExclusive) | 返回指定数组的指定范围作为其源的顺序Stream |
本篇文章就到这里了,希望能给你带来帮助,也希望您能够多多关注我的更多内容! 。
原文链接:https://blog.csdn.net/m0_50609545/article/details/117884151 。
最后此篇关于深入了解Java核心类库--Arrays类的文章就讲到这里了,如果你想了解更多关于深入了解Java核心类库--Arrays类的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我正在编写一个具有以下签名的 Java 方法。 void Logger(Method method, Object[] args); 如果一个方法(例如 ABC() )调用此方法 Logger,它应该
我是 Java 新手。 我的问题是我的 Java 程序找不到我试图用作的图像文件一个 JButton。 (目前这段代码什么也没做,因为我只是得到了想要的外观第一的)。这是我的主课 代码: packag
好的,今天我在接受采访,我已经编写 Java 代码多年了。采访中说“Java 垃圾收集是一个棘手的问题,我有几个 friend 一直在努力弄清楚。你在这方面做得怎么样?”。她是想骗我吗?还是我的一生都
我的 friend 给了我一个谜语让我解开。它是这样的: There are 100 people. Each one of them, in his turn, does the following
如果我将使用 Java 5 代码的应用程序编译成字节码,生成的 .class 文件是否能够在 Java 1.4 下运行? 如果后者可以工作并且我正在尝试在我的 Java 1.4 应用程序中使用 Jav
有关于why Java doesn't support unsigned types的问题以及一些关于处理无符号类型的问题。我做了一些搜索,似乎 Scala 也不支持无符号数据类型。限制是Java和S
我只是想知道在一个 java 版本中生成的字节码是否可以在其他 java 版本上运行 最佳答案 通常,字节码无需修改即可在 较新 版本的 Java 上运行。它不会在旧版本上运行,除非您使用特殊参数 (
我有一个关于在命令提示符下执行 java 程序的基本问题。 在某些机器上我们需要指定 -cp 。 (类路径)同时执行java程序 (test为java文件名与.class文件存在于同一目录下) jav
我已经阅读 StackOverflow 有一段时间了,现在我才鼓起勇气提出问题。我今年 20 岁,目前在我的家乡(罗马尼亚克卢日-纳波卡)就读 IT 大学。足以介绍:D。 基本上,我有一家提供簿记应用
我有 public JSONObject parseXML(String xml) { JSONObject jsonObject = XML.toJSONObject(xml); r
我已经在 Java 中实现了带有动态类型的简单解释语言。不幸的是我遇到了以下问题。测试时如下代码: def main() { def ks = Map[[1, 2]].keySet()
一直提示输入 1 到 10 的数字 - 结果应将 st、rd、th 和 nd 添加到数字中。编写一个程序,提示用户输入 1 到 10 之间的任意整数,然后以序数形式显示该整数并附加后缀。 public
我有这个 DownloadFile.java 并按预期下载该文件: import java.io.*; import java.net.URL; public class DownloadFile {
我想在 GUI 上添加延迟。我放置了 2 个 for 循环,然后重新绘制了一个标签,但这 2 个 for 循环一个接一个地执行,并且标签被重新绘制到最后一个。 我能做什么? for(int i=0;
我正在对对象 Student 的列表项进行一些测试,但是我更喜欢在 java 类对象中创建硬编码列表,然后从那里提取数据,而不是连接到数据库并在结果集中选择记录。然而,自从我这样做以来已经很长时间了,
我知道对象创建分为三个部分: 声明 实例化 初始化 classA{} classB extends classA{} classA obj = new classB(1,1); 实例化 它必须使用
我有兴趣使用 GPRS 构建车辆跟踪系统。但是,我有一些问题要问以前做过此操作的人: GPRS 是最好的技术吗?人们意识到任何问题吗? 我计划使用 Java/Java EE - 有更好的技术吗? 如果
我可以通过递归方法反转数组,例如:数组={1,2,3,4,5} 数组结果={5,4,3,2,1}但我的结果是相同的数组,我不知道为什么,请帮助我。 public class Recursion { p
有这样的标准方式吗? 包括 Java源代码-测试代码- Ant 或 Maven联合单元持续集成(可能是巡航控制)ClearCase 版本控制工具部署到应用服务器 最后我希望有一个自动构建和集成环境。
我什至不知道这是否可能,我非常怀疑它是否可能,但如果可以,您能告诉我怎么做吗?我只是想知道如何从打印机打印一些文本。 有什么想法吗? 最佳答案 这里有更简单的事情。 import javax.swin
我是一名优秀的程序员,十分优秀!