我正在使用ArrayList迭代来迭代Iteratot.While的元素,我在最后将一个元素添加到ArrayList中。但是得到了一个错误: ConcurrentModificationException。
如果我将ArrayList更改为Set,并试图在迭代结束时在set中添加元素,则不会引发错误。
Test.java
class Test {
public static void main() {
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(20);
al.add(30
关于这个问题的更多细节:我需要为一个字符串堆栈创建一个迭代器,它使用的是一个ArrayList,它只返回文件中所有小写的单词,并为此使用扫描程序。最后我必须对数组进行排序。我必须为这个问题创建两个单独的类。
,到目前为止,这就是我所拥有的:
import java.io.*;
import java.util.*;
public class StackClass
{
static StringSet stringStack;
static Scanner console = new Scanner(System.in);
public static void ma
我试图成为List<HashSet<Integer>> list = new ArrayList<>()的迭代器。
然而,我一直在跌倒。
这就是我尝试的迭代器Iterator<HashSet<Integer>> iterator = new list.iterator()
请谁帮帮我:
如何创建集合迭代器?
这是代码的一部分
public class Test {
private static List<HashSet<Integer>> basketList = new ArrayList<>()
我收到以下错误:
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextEntry(HashMap.java:810)
at java.util.HashMap$KeyIterator.next(HashMap.java:845)
at sudoku.Main.solve2(Main.java:143)
at sudoku.Main.next2(
我想看看一个数组中有多少唯一的值。它是唯一的,它不等于数组中向前或向后的任何其他字符串。这是我所拥有的,但它每次都会返回正确的值。
import java.util.ArrayList;
public class Answer {
public static int answer(String[] x) {
ArrayList<String> collectionOfCodes = new ArrayList<String>();
int arrayLength = x.length;
for (in
我试图在java中执行类似TSP的操作,并且在生成路径方面遇到了问题。我想用一个随机整数选择我的下一个“城市”,但是如果在使用这个整数之前,我要重新随机化它,并且在索引唯一之前不要进一步移动循环的索引。
这就是我的代码。
int citynum = 10;
double maxdist = 0;
int ants = 10;
Random random = new Random(500);
Random ran = new Random(citynum);
double dist;
double pair = 0;
ArrayList<TestCity> cities = new
Stackoverflow。
我尝试将ArrayList中的每个单词中的4个添加到ArrayList本身。我的ArrayList里有两个字符串。一个是"java“,另一个是"Program”。我正在尝试编写一个程序,每个单词总共添加4个单词。例如:我正在尝试添加4x java和4x程序。
这是我到目前为止所得到的。我不知道我做错了什么。任何帮助或提示都将不胜感激。
/*
* Write a method called quadList that takes an ArrayList of strings as a parameter
* and replaces
我想要一个程序,可以添加生成的随机数(唯一的,不重复的)到2个阵列交替…这就是我到目前为止所做的:
int n = 56,m=0;
int e=0,f=0;
int[] deck1 = new int[n];
int[] deck2 = new int[m];
Random rng = new Random();
List<Integer> generated = new ArrayList<Integer>();
for(int i=1;i<=53;i++)
{
while(true)
{
next = r
所以我正在尝试编写一个程序来计算int在数组中的出现次数。我尝试做的是创建一个列出唯一整数的方法,然后创建另一个将列表项与原始数组项进行比较的方法。
public List listUnique(int[] arr){
Arrays.sort(arr);
List <Integer> temp = new ArrayList<>();
int currentInt = 0;
for (int i = 0; i < arr.length; i++){
if(arr[i] != currentInt){
我在用Java写简单的塔防御游戏。下面是一种方法,它查找从派生(在构造函数中设置)到最近基的路径。
public int[] findPath(Field startField) {
ArrayList<TestMonster> monsrs = new ArrayList<TestMonster>();
TestMonster first = new TestMonster(startField.getCenter(), getStartingDirection(startField), new int[0]);
monsters.add(fi
我是这个网站的新手,所以如果我的问题或问题的风格有什么问题,请随时纠正我。
我需要在我的ShareCollection类中实现Interface,这样我就可以迭代这个类中的所有共享。当我用示例数据测试类时,它总是将'0‘作为大小返回,尽管我的集合中有两个共享(在我的示例中)。
这里是类+一个示例方法的代码,它返回一个错误:
public class ShareCollection implements Iterable<Share>{
private HashSet<Share> shares;
public ShareCollection()
我是Java的初学者:
for (int i = 0; i <= 4; i++) {
taken = false;
spun = wheel.spinWheel();
for (PrizeCard a : playerOne) {
if (spun.getID() == a.getID()) {
taken = true;
}
}
if (!taken) {
playerOne.add(spun);
} else {
i--;
}
}
对于这段代码,我尝试从另一个数组列表中随机选择对象,并将其添加到名为player
import java.util.ArrayList;
public class Subset { //Generate all subsets by generating all binary numbers
public static ArrayList<ArrayList<Integer>> getSubsets2(ArrayList<Integer> set) {
ArrayList<ArrayList<Integer>> allsubsets =
new ArrayList
我在Project上为问题7编写的快速程序一直在产生错误。
我不熟悉Java。我将问题7的Python代码转移到Java中,只是为了看看我是否能做到。我在教自己Java如何走在游戏的前面。
我的Python程序的代码是:
no = 2
print(3 % 2)
primes = [2]
while len(primes) != 10001:
no = no + 1
no2 = 0
for each in primes:
if no % each != 0:
no2 = no2 + 1
if len(prim
我有一个数组,我的目标是返回唯一的元素。我编写了对数组进行排序并在ArrayList中存储唯一元素的代码,但是它不是只返回[7],而是返回[2,5,7]。为什么会这样呢?
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
int [] arr = {2, 5, 14, 14, 2, 7, 5};
Arrays.sort(arr);
for (int i =0; i<arr.length-1; i++){
我有一个输入文件,如下所示 names.txt Maria Derek Erica
Livia Jack Anita
Kendall Maria Livia Derek
Jamie Jack
Thomson Erica 我想输出像这样。从名称中删除重复的单词 output.txt Maria Derek Erica
Livia Jack Anita
Kendall
Jamie
Thomson 我已经尝试读取由空格分隔的文件,然后将它们添加到ArrayList中,然后我就失去了生成输出的下一步操作。 import java.uti
我用Java写了下面的“分段筛子”程序。它需要筛选一定范围的数字,使用‘筛选素数’(素数数组变量)划掉复合数,然后返回没有被划掉的素数。代码如下:
public ArrayList<Integer> sieveWorker(int start, int last, ArrayList<Integer> primes) {
System.out.println("Thread started for range: " + start + "-" + last);
ArrayList<Integer> nonPr
我正在(或试图)在java中生成一个由Node和Edge组成的有向图,这个图代表了我所称的模块的依赖树(模块在我的程序中扮演了某种角色)。某些模块需要在程序中的其他模块之前运行,我使用有向图来表示这一点。模块(和Node)知道其他哪些模块必须先于它们。
目前,我获取并读取一个目录,并获得一个模块的ArrayList,然后将其转换为ArrayList of Node,我的问题是将这些node与node类addEdge(..)函数连接起来。当我调用我的函数(下面的代码) connectNodes(..)时,我想要在node之间创建边缘对象,这样我就可以遍历整个树,使用edge从node到node。
我有一个构造函数,它需要字符串的vararg作为参数。我需要分离这些字符串,并将它们添加到ArrayList中,该部分将完成我的方法。
代码如下:
private ArrayList<String> answers = new ArrayList<>();
private int index;
public Text(String ... str)
{
separateVararg(); // How to pass vararg in here
}
如何将这个vararg从构造函数作为参数传递给方法?
我需要将两个列表合并成一个,按升序,而不是重复,我认为我的代码非常接近,我只是遗漏了一些东西,我无法弄清楚。到目前为止,我的代码在我的合并方法中不能正常工作。我认为这与我的循环有关,但我就是无法绕过它。我目前的方法打印新的列表,但它不是以完美的增长顺序。我想知道如何让这个方法使用l1和l2的内容按升序打印我的合并列表。
**注:我不能使用任何内置数组排序方法。
谢谢!
import java.util.ArrayList;
import java.util.Random;
public class MergeLists {
public static ArrayList<In
嗨,我正在尝试添加这些值,如下面的代码所示。我搞错了。
for (String n2 : number ) {
List<String> ARRAY = new ArrayList<String>();
if (!ARRAY.contains(number)) {
ARRAY.add(number);
}}
但是在将数字添加到列表中时会出现错误。
错误
java:271: error: no suitable method found for add(List<String>