java Java整数ArrayList返回特定范围内的元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7414533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java integer ArrayList return elements within a specific range
提问by Krishnabhadra
I am having a java ArrayList of integer
我有一个整数的 java ArrayList
ArrayList <Integer> ageList = new ArrayList <Integer>();
I am having some integer values in this arrayList. I want to create a new ArrayList which has all elements in the above arrayList which passes a condition, like value between 25 and 35. ie if my ageList contains values
我在这个 arrayList 中有一些整数值。我想创建一个新的 ArrayList,它包含上述 arrayList 中的所有元素,它传递一个条件,例如 25 到 35 之间的值。即,如果我的 ageList 包含值
{12,234,45,33,28,56,27}
{12,234,45,33,28,56,27}
my newList should contain
我的 newList 应该包含
{33,28,27}
{33,28,27}
How can I achieve this?
我怎样才能做到这一点?
Note: I came to java from objective C background, where I used NSPredicate to easly do such kind of things..Any similiar methods in java?
注意:我是从客观 C 背景来到 java 的,在那里我使用 NSPredicate 来轻松地做这样的事情..java 中有任何类似的方法吗?
回答by aioobe
There is no "filter" facility in the standard API. (There is in Guavaand Apache Commonshowever.) You'll have to create a new list, loop through the original list and manually add the elements in range 25-35 to the new list.
标准 API 中没有“过滤器”工具。(但是在Guava和Apache Commons中有。)您必须创建一个新列表,循环遍历原始列表并手动将 25-35 范围内的元素添加到新列表中。
If you have no intention to keep the original list, you could remove the elements out of range using an Iterator
and the Iterator.remove
method.
如果您不打算保留原始列表,则可以使用 anIterator
和Iterator.remove
方法删除超出范围的元素。
If you have no duplicates in your list, you could use a TreeSet
in which case you could get all elements in a specific range using headSet
/ tailSet
.
如果列表中没有重复项,则可以使用 aTreeSet
在这种情况下,您可以使用headSet
/获取特定范围内的所有元素tailSet
。
Related question:(possibly even a duplicate actually)
相关问题:(实际上甚至可能是重复的)
回答by Jon Skeet
回答by user219882
You may use foreach loop
您可以使用 foreach 循环
List<Integer> newList = new ArrayList<Integer>();
for (Integer value : ageList) {
if (value >= 25 && value <= 35) {
newList.add(value);
}
}
回答by z3pr4h
Try this:
试试这个:
ArrayList<Integer> newList = new ArrayList<Integer>(ageList);
for (Iterator<Integer> it = newList.iterator(); it.hasNext();) {
int n = it.next();
if (n < 25 || n > 35)
it.remove();
}
回答by Mister Smith
Not possible with the standard JCF. You'll have to iterate.
使用标准 JCF 不可能。你将不得不迭代。
Consider using Guava. It has such kind of utilities.
考虑使用番石榴。它有这样的实用程序。
回答by Harry Joy
No there is no built in functionality in java [AFAIK].
不,java [AFAIK] 中没有内置功能。
The steps can be:
步骤可以是:
- Loop through ageList.
- Check for condition.
- If condition is success then add that element to new list or else do nothing.
- 循环遍历 ageList。
- 检查状况。
- 如果条件成功,则将该元素添加到新列表中,否则什么都不做。
Sample code:
示例代码:
List<Integer> ageList = new ArrayList<Integer>();
//-- add data in ageList
List<Integer> newList = new ArrayList<Integer>();
for (Integer integer : ageList) {
if(integer >= 25 && integer <= 35)
newList.add(integer);
}
回答by Charles Goodwin
Iterate over it and just add the ones that match? It's 3 lines of code... 4 including the new ArrayList declration.
迭代它并只添加匹配的那些?这是 3 行代码... 4 包括新的 ArrayList 声明。