如何ArrayList从一个线程循环,同时从其他线程添加它?
1)我有一个循环ArrayList并执行一些检查的线程.
2)我有第二个线程添加到结尾ArrayList.
我有信号量,当第二个线程运行时暂停第一个线程,但我该如何实现:
当第二个线程添加某些东西时ArrayList,第一个线程将从暂停的点继续循环而不抛出java.util.ConcurrentModificationException?
我试图允许"Admin"类创建多个数组.但是,每次运行此循环时,它似乎都会删除先前创建的数组.
do {
System.out.println("\nWhat do you want "
+ "the name of your line to be?: ");
String lineName= keyboard1.nextLine();
System.out.println("\n\nCongratulations! \n"+ "Your "+ lineName +
" queue has been created! \n");
//Create an array that will hold "User" information.
ArrayList<User>queue=new ArrayList<>();
//Create an array of arrays to allow "User" to see all "lines" available to them.
ArrayList<String>totalLines=new ArrayList<>();
`//Add the array name to the array of arrays.
`totalLines.add(lineName);
//Show Admin all of their lines.
System.out.println("Here is a list of …Run Code Online (Sandbox Code Playgroud) 我正在创建一个具有ArrayList的类,所以当我调用构造函数时,它会初始化该数组:
public class ElementsList<E>
{
private ArrayList<E> list;
public ElementsList()
{
list = new ArrayList<E>;
}
}
Run Code Online (Sandbox Code Playgroud)
我不明白为什么当你创建构造函数时,你真的不必提到这个类将保留某些元素的事实.我的意思是你不必这样做:
public ElementsList<E>{
list = new ArrayList<E>;
}
Run Code Online (Sandbox Code Playgroud)
LONG STORY SHORT:为什么类的名称是ElementsList<E>,并且构造函数没有该<E>部分?
如果我有类似的东西:
ArrayList<Data> mList = new ArrayList();
...
public void setList(ArrayList<Data> list) {
mList = list;
}
public void updateList(ArrayList<Data> list) {
mList.clear();
mList.addAll(list);
}
Run Code Online (Sandbox Code Playgroud)
哪个更有效,setList()或者updateList()为什么?什么是最佳做法?
编辑:
不要混淆这个问题.
如何在Arraylist中声明String []?例如,我有一个未知/不同数量的电影/预告片对.因此,对于arraylist中的每个项目,我希望有一个带有电影名称的String []和预告片url.我尝试了类似下面的一些东西,但它们不起作用:
private ArrayList<String[2]> movieArray= new ArrayList<>();
Run Code Online (Sandbox Code Playgroud) 我有一些代码,我注意到迭代通过ArrayList的进度随着时间的推移变得非常慢.似乎导致问题的代码如下:
public boolean isWordOfficial(String word){
return this.wordList.get(this.stringWordList.indexOf(word)).isWordOfficial();
}
Run Code Online (Sandbox Code Playgroud)
在访问两个arraylists方面,我不知道这些代码有什么问题吗?
我有这个类的构造函数以String和Arraylist作为参数;
class Foo{
String ide;
ArrayList list;
public TypeOperator(String ide, ArrayList list){
this.ide = ide;
this.list = list;
}
}
Run Code Online (Sandbox Code Playgroud)
所以当我像下面这样初始化类时:
Foo one = new Foo("boo", []);
Run Code Online (Sandbox Code Playgroud)
它给出了错误。好吧,我可以这样做以使其工作:
ArrayList empty = new ArrayList();
Foo one = new Foo("boo", empty);
Run Code Online (Sandbox Code Playgroud)
有什么理由使第一个解决方案不起作用?还是有办法进行一些更改以使其起作用?谢谢
所以我是Java的新手,并且在数组之前开始使用ArrayLists.我喜欢它们与Python列表更相似,并且理解ArrayLists只包含对象而不是基元,在我看来,它们对包含多个类型的列表更有用.我写这篇文章是为了展示我如何制作一副牌(一切都是静态的,只是为了这个例子):
public class Deck {
private static Scanner input = new Scanner(System.in);
private static Random newRan = new Random();
private static final List RANKS = new ArrayList(Arrays.asList("ace", 2, 3, 4, 5, 6, 7, 8, 9,
10, "jack", "queen", "king"));
private static final List SUITS = new ArrayList(Arrays.asList("hearts", "diamonds", "clubs",
"spades"));
private static List makeDeck() {
List result = new ArrayList();
for (Object i : SUITS) {
for (Object j : RANKS) {
result.add(Arrays.asList(i, j));
}
}
return result;
}
public …Run Code Online (Sandbox Code Playgroud) 我想转换我的array2D:
int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
Run Code Online (Sandbox Code Playgroud)
到列表:
List<String> numbers = new List<string>(array2d.ToString());
Run Code Online (Sandbox Code Playgroud) 我刚读完Head First Java的第16章,它是关于集合和泛型的使用.本章末尾有一个"Be be compiler"练习,提供了9行代码,挑战在于哪些代码行能够成功编译.给出的代码行如下: -
ArrayList<Dog> dogs1 = new ArrayList<Animal>();
ArrayList<Animal> animals1 = new ArrayList<Dog>();
List<Animal> list = new ArrayList<Animal>();
ArrayList<Dog> dogs = new ArrayList<Dog>(); //The book says this line will not compile
ArrayList<Animal> animals = dogs;
List<Dog> dogList = dogs;
ArrayList<Object> objects = new ArrayList<Object>();
List<Object> ObjList = objects;
ArrayList<Object> objs = new ArrayList<Dog>();
Run Code Online (Sandbox Code Playgroud)
解决方案声明第3行,第6行,第7行和第8行将编译但是当我尝试自己编译代码行时,我发现第4行也成功编译.更重要的是,第6行代码依赖于第4行代码编译,因为它引用了第4行的ArrayList!
我想知道是否有其他人做过这个"成为编译器"的练习在完成它时注意到这个问题,或者是否有人可以知道为什么作者会将第四行代码从解决方案中删除,即使它编译精细.
非常感谢您提供的任何帮助.