我一直只是一个人使用:
List<String> names = new ArrayList<>();
Run Code Online (Sandbox Code Playgroud)
我使用接口作为可移植性的类型名称,因此当我问这些问题时,我可以重新编写代码.
何时应该LinkedList使用,ArrayList反之亦然?
如何将数组转换为Java中的列表?
我使用Arrays.asList()但行为(和签名)以某种方式从Java SE 1.4.2(现在存档中的文档)更改为8,我在网上找到的大多数片段使用1.4.2行为.
例如:
int[] spam = new int[] { 1, 2, 3 };
Arrays.asList(spam)
Run Code Online (Sandbox Code Playgroud)
在许多情况下,它应该很容易被发现,但有时它可能会被忽视:
Assert.assertTrue(Arrays.asList(spam).indexOf(4) == -1);
Run Code Online (Sandbox Code Playgroud) 从集合框架概述:
不支持修改操作(例如
add,remove和clear)的集合称为不可修改.不可修改的集合是可修改的.另外保证
Collection对象中没有可见变化的集合称为不可变.不可变的集合是可变的.
我无法理解这种区别.这里不可修改和不可变
的区别是什么?
我有一个集合c1<MyClass>和一个数组a<MyClass>.我试图将数组转换为集合c2并做c1.removeAll(c2),但这引发了UnsupportedOperationException.我发现asList()Arrays类返回了Arrays.ArrayList类,并且该类继承了其实现抛出的removeAll()from .AbstractList()UnsupportedOperationException
Myclass la[] = getMyClass();
Collection c = Arrays.asList(la);
c.removeAll(thisAllreadyExistingMyClass);
Run Code Online (Sandbox Code Playgroud)
有没有办法删除元素?请帮忙
在Java中,我有一个字符串的ArrayList,如:
[,Hi, ,How,are,you]
Run Code Online (Sandbox Code Playgroud)
我想删除null和empty元素,如何更改它,所以它是这样的:
[Hi,How,are,you]
Run Code Online (Sandbox Code Playgroud) 我试图从a中删除元素List并获取java.lang.UnsupportedOperationException.
public <T extends Object> void findDuplicates(
String title, Multimap<T, ChunkId> map) {
for (T key : map.keySet()) {
Collection<ChunkId> ids = map.get(key);
List<ChunkId> idList = (Arrays.asList(ids.toArray(new ChunkId[0])));
removeUsedIds(idList);
Collections.sort(idList);
//...
}
}
private void removeUsedIds(List<ChunkId> idList) {
// decrement counter to avoid indexing changed parts of list
for (int i = idList.size() - 1; i >= 0; i--) {
if (usedIdSet.contains(idList.get(i))) {
idList.remove(i); // **** Exception thrown here
}
}
}
Run Code Online (Sandbox Code Playgroud)
我明白了
Exception in thread …Run Code Online (Sandbox Code Playgroud) 我得到Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0以下代码的例外.但无法理解为什么.
public class App {
public static void main(String[] args) {
ArrayList<String> s = new ArrayList<>();
//Set index deliberately as 1 (not zero)
s.add(1,"Elephant");
System.out.println(s.size());
}
}
Run Code Online (Sandbox Code Playgroud)
我可以使它工作,但我试图理解这些概念,所以我将声明改为下面,但也没有工作.
ArrayList<String> s = new ArrayList<>(10)
Run Code Online (Sandbox Code Playgroud) 我的问题是当Recycler View向上和向下滚动时
我在这里如何在Fragment中设置My Recycler视图.
private void setRecyclerView() {
recyclerView.setHasFixedSize(true);
StaggeredGridLayoutManager layoutManager =
new StaggeredGridLayoutManager(2, 1);
recyclerView.setLayoutManager(layoutManager);
adapter = new TwitterTweetAdapter(getActivity());
// setting every thing false in item animator
recyclerView.setItemAnimator(new RecyclerView.ItemAnimator() {
@Override
public void runPendingAnimations() {
}
@Override
public boolean animateRemove(RecyclerView.ViewHolder viewHolder) {
return false;
}
@Override
public boolean animateAdd(RecyclerView.ViewHolder viewHolder) {
return false;
}
@Override
public boolean animateMove(RecyclerView.ViewHolder viewHolder, int i, int i2, int i3, int i4) {
return false;
}
@Override
public boolean animateChange(RecyclerView.ViewHolder viewHolder, …Run Code Online (Sandbox Code Playgroud) android adapter fragment android-viewholder android-recyclerview
初始起点
我有一个List1000个Person对象,我想插入一个Extractor来监听任何Person对象中的属性更改(ObservableList稍后将附加到a TableView).
所以我的代码就像:
ObservableList<Person> observablePersons = FXCollections.observableList(personlist,
personextractor);
Run Code Online (Sandbox Code Playgroud)
错误信息
但是当我尝试向这个ObservableList observablePersons添加一个新人对象时,我遇到了这个错误:
run:
Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.AbstractList.add(AbstractList.java:148)
at com.sun.javafx.collections.ObservableListWrapper.doAdd(ObservableListWrapper.java:101)
at javafx.collections.ModifiableObservableListBase.add(ModifiableObservableListBase.java:151)
at java.util.AbstractList.add(AbstractList.java:108)
at test.listchangelistener.listChangeDemo.main(listChangeDemo.java:72)
Java Result: 1
Run Code Online (Sandbox Code Playgroud)
您能否告诉我为什么会遇到此错误消息?我的java版本是jdk1.8.0_91(32位)
人类
package test.listchangelistener;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
class Person {
private final IntegerProperty age = new SimpleIntegerProperty();
private final StringProperty name = new SimpleStringProperty();
public Person(String name, Integer age) {
setName(name); …Run Code Online (Sandbox Code Playgroud) java ×9
arrays ×3
arraylist ×2
collections ×2
list ×2
adapter ×1
android ×1
fragment ×1
immutability ×1
javafx ×1
linked-list ×1