我们都知道你不能这样做:
for (Object i : l) {
if (condition(i)) {
l.remove(i);
}
}
Run Code Online (Sandbox Code Playgroud)
ConcurrentModificationException等等......这显然有时起作用,但并非总是如此.这是一些特定的代码:
public static void main(String[] args) {
Collection<Integer> l = new ArrayList<>();
for (int i = 0; i < 10; ++i) {
l.add(4);
l.add(5);
l.add(6);
}
for (int i : l) {
if (i == 5) {
l.remove(i);
}
}
System.out.println(l);
}
Run Code Online (Sandbox Code Playgroud)
当然,这会导致:
Exception in thread "main" java.util.ConcurrentModificationException
Run Code Online (Sandbox Code Playgroud)
...即使多线程没有这样做......无论如何.
什么是这个问题的最佳解决方案?如何在循环中从集合中删除项而不抛出此异常?
我也在Collection这里使用任意,不一定是ArrayList,所以你不能依赖get.
OK,让我们说,我有充满了一个数组{"管","是","好玩"},然后我有一个JTextField,如果我输入任一这些命令做一些事情之一,如果没有得到这样的消息说"没有找到指令".
我试图寻找在Java文档,但所有我得到的东西,我不希望这样的问题和东西...所以,这是怎么完成的?我知道有一个"数组内"功能,但我把两者结合起来并不太好.
谢谢.
这是我到目前为止:
String[] dan = {"Red", "Orange", "Yellow", "Green", "Blue", "Violet", "Orange", "Blue"};
boolean contains = dan.contains(say.getText());
Run Code Online (Sandbox Code Playgroud)
但是我在dan.contains中找不到符号
鉴于:
String[] directions = {"UP","DOWN","RIGHT","LEFT","up","down","right","left"};
String input = "Up";
Run Code Online (Sandbox Code Playgroud)
如何验证输入stdin是否在directions数组内?
我可以制作一个循环并input使用相同的方式检查每个项目,但我正在寻找一种更优雅的方式.
问候,罗恩
我有一个字符串,我必须解析不同的关键字.例如,我有字符串:
"我会在123woods来见你"
我的关键字是
'123woods''森林'
我应该在每次有比赛时报告.还应考虑多次出现.然而,对于这个,我应该只在123woods匹配,而不是在树林.这消除了使用String.contains()方法.此外,我应该能够有一个列表/一组关键字,并同时检查它们的发生.在这个例子中,如果我有'123woods'和'come',我应该两次出现.方法执行在大文本上应该有点快.
我的想法是使用StringTokenizer,但我不确定它是否会表现良好.有什么建议?
我想知道在Java中是否有办法做这样的事情:
if(word in stringArray) {
...
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以为此创建一个函数,但我只是想知道Java是否已经有了这个功能.
谢谢!
我一直在这里搜索一段时间,但一直未能找到答案.
我基本上需要使用数组来完成大学的这项任务.然后我应该检查输入(也是一个String)匹配String数组中存储的内容.
我知道可以通过使用.equals()方法轻松比较Strings.但是,相同的方法不适用于String数组.
我为StackOverflow创建了以下代码示例,因此您可以使用它来向我解释它,如果您愿意的话.
我究竟做错了什么?
import java.util.Scanner;
class IdiocyCentral {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
/*Prints out the welcome message at the top of the screen*/
System.out.printf("%55s", "**WELCOME TO IDIOCY CENTRAL**\n");
System.out.printf("%55s", "=================================\n");
String [] codes = {"G22", "K13", "I30", "S20"};
System.out.printf("%5s%5s%5s%5s\n", codes[0], codes[1], codes[2], codes[3]);
System.out.printf("Enter one of the above!\n");
String usercode = in.nextLine();
if (codes.equals(usercode)) {
System.out.printf("What's the matter with you?\n");
}
else {
System.out.printf("Youda man!");
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果以前曾经问过这个问题,我很抱歉,如果它是一个双重问题,我会将其删除.
这就是我所拥有的:
char[] charArray = new char[] {'h','e','l','l','o'};
Run Code Online (Sandbox Code Playgroud)
我想写一些东西:
if(!charArray contains 'q'){
break;
}
Run Code Online (Sandbox Code Playgroud)
我意识到.contains()不能在这里使用.我只是用"包含"来说明我正在尝试做什么.
在java中,我们有任何方法可以找到特定字符串是字符串数组的一部分.我可以在循环中做,我想避免.
例如
String [] array = {"AA","BB","CC" };
string x = "BB"
Run Code Online (Sandbox Code Playgroud)
我想要一个
if (some condition to tell whether x is part of array) {
do something
} else {
do soemthing
}
Run Code Online (Sandbox Code Playgroud) 我在我的代码中使用了Arrays.asList().contains()方法,如上面的答案所示:如何测试数组是否包含某个值?,所以我将在代码中使用Arrays.asList().
但是,编译器拒绝以下代码.是因为我的素数数组使用原语而不是引用类型?由于自动装箱,我不这么认为,但我只想查一下.
import java.math.*;
import java.util.ArrayList;
import java.util.Arrays;
public class .... {
public static void main(String[] args) {
int[] primes = formPrimes(15);
ArrayList<Integer> primes1 = new ArrayList<Integer>(Arrays.asList(primes));
// Rest of code...
}
public static int[] formPrimes(int n) {
// Code that returns an array of integers
}
}
Run Code Online (Sandbox Code Playgroud)
我收到一个错误,找不到符号错误.
symbol:构造函数ArrayList(java.util.List)
location:类java.util.ArrayList ArrayList primes1 = new ArrayList(Arrays.asList(primes));
基本上,我有一个函数返回一个整数数组,我想将它转换为数组列表,我使用ArrayList构造函数遇到了麻烦.
我需要提供从高度无结构的Web服务中挑选的高度结构化的信息.为了正确显示信息,我必须做很多String匹配和重复删除,以确保我选择正确的元素组合.
我的一个挑战涉及确定String是否在字符串数组中.
我的梦想是做"searchString.isIn(stringArray);" 但我意识到String类没有提供.
除了这个存根之外还有更有效的方法吗?:
private boolean isIn(String searchString, String[] searchArray)
{
for(String singleString : searchArray)
{
if (singleString.equals(searchString)
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
谢谢!