考虑下面的代码
List<Integer> l=new ArrayList();
Set<Integer> l=new HashSet();
Run Code Online (Sandbox Code Playgroud)
以下哪个 for 循环是最佳的
for(Integer i: some arrray){
if(!l.contains(i))
l.add(i);
}
Run Code Online (Sandbox Code Playgroud)
或者
for(Integer i :some array){
s.add(i);
}
Run Code Online (Sandbox Code Playgroud) 我有一封List<String> emails
包含电子邮件,长度为n
,另一个List<String> keywords
包含关键字,长度相同。这些列表应满足以下条件: 对于每个索引i
emails.get(i).contains(keywords.get(i))
所以,如果emails.get(0) == "quick brown fox"
,那么keywords.get(0) == "fox"
。
如果emails.get(5) == "foo bar"
,那么keywords.get(5) == "foo"
。
如何检查(除了for
循环之外)每封电子邮件都包含关键字?
我将如何使用流来实现与下面相同的结果?我试图通过首先迭代 TickQueue (队列实现)并对 tot 求和,然后除以计数器值来找到平均值,从而找到此“tot”值的平均值。
int counter = 0;
double tot = 0;
for (Tick t: TickQueue)
{
if ((t.getAskPrice() == 0 && t.getBidPrice() == 0) || (t.getAskPrice() == 0) || (t.getBidPrice() == 0))
{
tot += 0;
}
else
{
tot += (t.getAskPrice() - t.getBidPrice());
counter++;
}
}
double avg = tot/counter;
Run Code Online (Sandbox Code Playgroud) 允许用户使用字符串数组.他们可以向数组添加字符串,从数组中删除字符串,搜索数组中的字符串,最终他们将能够对数组进行排序.分类是搞砸我的原因.我尝试过几种不同的方法.第一种方法是将数组转换为ArrayList,并使用Collections对ArrayList进行排序,ArrayList将转换回静态类数组.它不起作用.我尝试的第二种方法是迭代数组并尝试仅排序用户添加的字符串而不是数组中的所有内容(因为数组中有一些空值).也许我应该遍历数组,然后将非null值存储到一个新的数组中,然后我可以对它进行排序?但是如果我想在排序新数组后添加更多字符串呢?这就是为什么我停止了第二个解决方案.第三次尝试是在我的数组上使用Arrays.sort()但由于某种原因它不起作用.
这是一个例外:
Exception in thread "main" java.lang.NullPointerException
at java.util.ComparableTimSort.countRunAndMakeAscending(ComparableTimSort.java:290)
at java.util.ComparableTimSort.sort(ComparableTimSort.java:157)
at java.util.ComparableTimSort.sort(ComparableTimSort.java:146)
at java.util.Arrays.sort(Arrays.java:472)
at java.util.Collections.sort(Collections.java:155)
at testingSearch.sortArray(testingSearch.java:93)
at testingSearch.main(testingSearch.java:42)
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
import java.util.Scanner;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class testingSearch {
static String[] strArray;
static {
strArray = new String[5];
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while(true){
System.out.println("1. Add string to the string array.");
System.out.println("2. Remove string from the string array.");
System.out.println("3. Display strings in string array.");
System.out.println("4. Search …
Run Code Online (Sandbox Code Playgroud) 它是什么意思它是一个数组或它是一个列表类型字符串
List<String> list = new ArrayList<String>(); // why parenthesis
List<String> removeList = new ArrayList<String>();//why parenthesis
Run Code Online (Sandbox Code Playgroud)
这个方法那么List和Collection和arrys之间有什么区别呢
// what mean this collection
private void removeColors(Collection<String> collection1,Collection<String> collection2)
{
Iterator<String> iterator = collection1.iterator();//what does mean
while(iterator.hasNext())//what does mean
if(collection2.contains(iterator.next()))//what does mean
iterator.remove();//what does mean
}
Run Code Online (Sandbox Code Playgroud) 所以我知道Collections是一个实用程序类,Collection是一个基本接口.但是,我仍然不明白为什么集合中的方法不能放入集合中?这只是为了让事情变得更容易吗?
集合和集合中的方法之间是否存在差异?也许某些方法是由不同的集合实现的?因此对于更通用的方法,它们被抛入实用程序类集合中?
我想我只是很好奇你如何决定将哪些内容投入到收藏或收藏中?谁能提供一些这些例子?
在可比性中,只能创建一个排序序列,而在比较器中,可以创建许多排序序列。排序顺序在这里到底是什么意思。
我正在将地图中的所有键放入 HashSet。
我希望能够将其传输到 ArrayList 中。
我有一个具有双值的元组列表:
List<Tuple<string, string>> Descriptions;
Run Code Online (Sandbox Code Playgroud)
我一直在为此添加内容,如下所示:
Descriptions.Add (new Tuple<string, string> ("max", "some description"));
Descriptions.Add (new Tuple<string, string> ("joe", "some description"));
Descriptions.Add (new Tuple<string, string> ("jane", "some description"));
Descriptions.Add (new Tuple<string, string> ("max", "some other description"));
Run Code Online (Sandbox Code Playgroud)
我想使用Linq检索一个列表,其中Item1
元组是一个特定的值,比如说"max"
.我可以使用这段代码:
var s = Descriptions.Where (x => x.Item1 == "max");
Run Code Online (Sandbox Code Playgroud)
但是这将分配小号元组,这是我不希望的列表.我只想要一个描述字符串的列表,也就是说,它应该返回一个list<string>
包含与Item1
字符串相关的所有描述"max"
.
我有List<List<String>>
。我想根据内部List的特定元素将其转换为Map。
我试过了
ddList.stream().flatMap(x -> x.stream()
.collect(Collectors.toMap(Function.identity(), String::length)));
Run Code Online (Sandbox Code Playgroud)
它不起作用。这是怎么了?
collections ×10
java ×9
arraylist ×2
java-8 ×2
java-stream ×2
list ×2
sorting ×2
api ×1
arrays ×1
c# ×1
comparable ×1
comparator ×1
lambda ×1
linq ×1
loops ×1
string ×1
tuples ×1