Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9480644

Browse files
committed
"如何判断数组Array是否包含指定的值"有重复的两篇文档,整合到一起
1 parent 58260dc commit 9480644

File tree

3 files changed

+16
-21
lines changed

3 files changed

+16
-21
lines changed

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ stackoverflow-Java-top-qa
1717
* [将数组转换为List](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/create-arraylist-arraylistt-from-array-t.md)
1818
* [如何遍历map对象](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/iterate-through-a-hashmap.md)
1919
* [public,protected,private,不加修饰符。有什么区别呢?](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/in-java-whats-the-difference-between-public-default-protected-and-private.md)
20-
* [如何判断数组Array是否包含指定的值?](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/in-java-how-can-i-test-if-an-array-contains-a-certain-value.md)
20+
* [如何测试一个数组是否包含指定的值?](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/How-can-I-test-if-an-array-contains-a-certain-value.md)
2121
* [重写(Override)equlas和hashCode方法时应考虑的问题](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/what-issues-should-be-considered-when-overriding-equals-and-hashcode-in-java.md)
2222
* [从一个多层嵌套循环中直接跳出](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/breaking-out-of-nested-loops-in-java.md)
2323
* [如何将String转换为Int](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/converting-string-to-int-in-java.md)

‎contents/How-can-I-test-if-an-array-contains-a-certain-value.md‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,23 @@ public static final String[] VALUES = new String[] {"AB","BC","CD","AE"};
66
```
77
现在制定一个值 s,有哪些比较好的方式,判断这个数组 VALUES 是否包含值 s?
88

9-
简单且优雅的方法:
10-
```
11-
Arrays.asList(yourArray).contains(yourValue);
9+
## 简单且优雅的方法:
10+
11+
1. Arrays.asList(...).contains(...)
12+
13+
2. 使用 Apache Commons Lang包中的ArrayUtils.contains
14+
15+
16+
```java
17+
String[] fieldsToInclude = { "id", "name", "location" };
18+
19+
if ( ArrayUtils.contains( fieldsToInclude, "id" ) ) {
20+
// Do some stuff.
21+
}
1222
```
1323

24+
25+
## 自己写逻辑
1426
问题的本质,其实是一个查找的问题,即查找一个数组是否包含某个值。对于原始类型,若是无序的数组,可以直接写一个 for 循环:
1527
```
1628
public static boolean useLoop(String[] arr, String targetValue) {

‎contents/in-java-how-can-i-test-if-an-array-contains-a-certain-value.md‎

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /