我有一个ArrayList我想完全输出为String.基本上我想按顺序输出它,使用toString由制表符分隔的每个元素.有没有快速的方法来做到这一点?你可以循环它(或删除每个元素)并将它连接到一个字符串,但我认为这将是非常缓慢的.
两个数据结构ArrayList和Vector之间有什么区别,你应该在哪里使用它们?
我在java中有一个双打列表,我想按降序排序ArrayList.
输入ArrayList如下:
List<Double> testList = new ArrayList();
testList.add(0.5);
testList.add(0.2);
testList.add(0.9);
testList.add(0.1);
testList.add(0.1);
testList.add(0.1);
testList.add(0.54);
testList.add(0.71);
testList.add(0.71);
testList.add(0.71);
testList.add(0.92);
testList.add(0.12);
testList.add(0.65);
testList.add(0.34);
testList.add(0.62);
Run Code Online (Sandbox Code Playgroud)
输出应该是这样的
0.92
0.9
0.71
0.71
0.71
0.65
0.62
0.54
0.5
0.34
0.2
0.12
0.1
0.1
0.1
Run Code Online (Sandbox Code Playgroud) 我试图删除一些元素ArrayList迭代它像这样:
for (String str : myArrayList) {
if (someCondition) {
myArrayList.remove(str);
}
}
Run Code Online (Sandbox Code Playgroud)
当然,我ConcurrentModificationException试图在迭代时同时从列表中删除项目时得到一个myArrayList.有没有一些简单的解决方案来解决这个问题?
反转此ArrayList的最简单方法是什么?
ArrayList<Integer> aList = new ArrayList<>();
//Add elements to ArrayList object
aList.add("1");
aList.add("2");
aList.add("3");
aList.add("4");
aList.add("5");
while (aList.listIterator().hasPrevious())
Log.d("reverse", "" + aList.listIterator().previous());
Run Code Online (Sandbox Code Playgroud) 我有一个ArrayList自定义对象.每个自定义对象都包含各种字符串和数字.即使用户离开活动然后想要稍后返回,我也需要数组停留,但是在应用程序完全关闭后我不需要数组可用.我通过这种方式保存了很多其他对象,SharedPreferences但我无法弄清楚如何以这种方式保存整个数组.这可能吗?也许SharedPreferences这不是解决这个问题的方法吗?有更简单的方法吗?
假设我创建了一个对象并将其添加到我的对象中ArrayList.如果我然后使用完全相同的构造函数输入创建另一个对象,那么该contains()方法是否会将两个对象评估为相同?假设构造函数对输入没有做任何有趣的事情,并且存储在两个对象中的变量是相同的.
ArrayList<Thing> basket = new ArrayList<Thing>();
Thing thing = new Thing(100);
basket.add(thing);
Thing another = new Thing(100);
basket.contains(another); // true or false?
Run Code Online (Sandbox Code Playgroud)
class Thing {
public int value;
public Thing (int x) {
value = x;
}
equals (Thing x) {
if (x.value == value) return true;
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
这是class应该如何实现contains()回归true?
是否有一个实用方法可以在一行中执行此操作?我无法在任何地方找到它Collections,或者List.
public List<String> stringToOneElementList(String s) {
List<String> list = new ArrayList<String>();
list.add(s);
return list;
}
Run Code Online (Sandbox Code Playgroud)
除非我打算在上面放上花哨的轮辋,否则我不想重新发明轮子.
嗯......类型可以T,而不是String.但你明白了.(所有空检查,安全检查......等)
假设arraylist被定义为ArrayList<String> arraylist,arraylist.removeAll(arraylist)相当于arraylist.clear()?
如果是这样,我可以假设该clear()方法更有效地清空数组列表吗?
使用中是否有任何警告arraylist.removeAll(arraylist)而不是arraylist.clear()?
我正在尝试使用以下代码将包含Integer对象的ArrayList转换为原始int [],但它会引发编译时错误.是否可以用Java进行转换?
List<Integer> x = new ArrayList<Integer>();
int[] n = (int[])x.toArray(int[x.size()]);
Run Code Online (Sandbox Code Playgroud)