Java 集合对象出现 UnsupportedOperationException
Java UnsupportedOperationException with Collection objects
我在 JDK 1.7 中使用 Java 集合时出错:
我在这一行得到了这个异常:proposalStatuses.addAll(getAllSubmittedStatuses())
java.lang.UnsupportedOperationException
at java.util.AbstractList.add(Unknown Source)
at java.util.AbstractList.add(Unknown Source)
at java.util.AbstractCollection.addAll(Unknown Source)
正在尝试将集合添加到列表
/**
* Gets the all submitted statuses.
*
* @return the all submitted statuses
*/
private Collection<ProposalStatus> getAllSubmittedStatuses() {
return Arrays.asList(
ProposalStatus.SAVED_TO_IOS
, ProposalStatus.SENDED_TO_IOS_IN_PROGRESS
);
}
/**
* Gets the all received statuses.
*
* @return the all received statuses
*/
private Collection<ProposalStatus> getAllReceivedStatuses() {
Collection<ProposalStatus> proposalStatuses =
Arrays.asList(
ProposalStatus.RECEIVED_BY_IOS
, ProposalStatus.SUBMITTED_TO_IOS
, ProposalStatus.RECEIVED_IOS
);
proposalStatuses.addAll(getAllSubmittedStatuses());
return proposalStatuses;
}
为了更实际地解释一下,我正在使用您的代码:
private Collection<ProposalStatus> getAllSubmittedStatuses() {
// This returns a list that cannot be modified, fixed size
return Arrays.asList(
ProposalStatus.SAVED_TO_IOS
, ProposalStatus.SENDED_TO_IOS_IN_PROGRESS
);
}
/**
* Gets the all received statuses.
*
* @return the all received statuses
*/
private Collection<ProposalStatus> getAllReceivedStatuses() {
// proposalStatuses will be a fixed-size list so no changing
Collection<ProposalStatus> proposalStatuses =
Arrays.asList(
ProposalStatus.RECEIVED_BY_IOS
, ProposalStatus.SUBMITTED_TO_IOS
, ProposalStatus.RECEIVED_IOS
);
// This will not be possible, also you cannot remove anything.
proposalStatuses.addAll(getAllSubmittedStatuses());
return proposalStatuses;
}
为了您的目的,我将执行以下操作:
return new ArrayList<ProposalStatus>(Arrays.asList(ProposalStatus.SAVED_TO_IOS,ProposalStatus.SENDED_TO_IOS_IN_PROGRESS)
这应该可以帮助您获取集合对象。
Arrays.asList()
returns 一个不可变列表,您无法修改。
更具体地说,add()、addAll() 和 remove() 方法未实现。
来自 javadoc of Arrays.asList()
(强调我的):
Returns a fixed-size list backed by the specified array
简而言之:您不能从这样的列表中 .add*()
或 .remove*()
!您必须使用另一个可修改的 List 实现(例如 ArrayList
)。
我在 JDK 1.7 中使用 Java 集合时出错: 我在这一行得到了这个异常:proposalStatuses.addAll(getAllSubmittedStatuses())
java.lang.UnsupportedOperationException
at java.util.AbstractList.add(Unknown Source)
at java.util.AbstractList.add(Unknown Source)
at java.util.AbstractCollection.addAll(Unknown Source)
正在尝试将集合添加到列表
/**
* Gets the all submitted statuses.
*
* @return the all submitted statuses
*/
private Collection<ProposalStatus> getAllSubmittedStatuses() {
return Arrays.asList(
ProposalStatus.SAVED_TO_IOS
, ProposalStatus.SENDED_TO_IOS_IN_PROGRESS
);
}
/**
* Gets the all received statuses.
*
* @return the all received statuses
*/
private Collection<ProposalStatus> getAllReceivedStatuses() {
Collection<ProposalStatus> proposalStatuses =
Arrays.asList(
ProposalStatus.RECEIVED_BY_IOS
, ProposalStatus.SUBMITTED_TO_IOS
, ProposalStatus.RECEIVED_IOS
);
proposalStatuses.addAll(getAllSubmittedStatuses());
return proposalStatuses;
}
为了更实际地解释一下,我正在使用您的代码:
private Collection<ProposalStatus> getAllSubmittedStatuses() {
// This returns a list that cannot be modified, fixed size
return Arrays.asList(
ProposalStatus.SAVED_TO_IOS
, ProposalStatus.SENDED_TO_IOS_IN_PROGRESS
);
}
/**
* Gets the all received statuses.
*
* @return the all received statuses
*/
private Collection<ProposalStatus> getAllReceivedStatuses() {
// proposalStatuses will be a fixed-size list so no changing
Collection<ProposalStatus> proposalStatuses =
Arrays.asList(
ProposalStatus.RECEIVED_BY_IOS
, ProposalStatus.SUBMITTED_TO_IOS
, ProposalStatus.RECEIVED_IOS
);
// This will not be possible, also you cannot remove anything.
proposalStatuses.addAll(getAllSubmittedStatuses());
return proposalStatuses;
}
为了您的目的,我将执行以下操作:
return new ArrayList<ProposalStatus>(Arrays.asList(ProposalStatus.SAVED_TO_IOS,ProposalStatus.SENDED_TO_IOS_IN_PROGRESS)
这应该可以帮助您获取集合对象。
Arrays.asList()
returns 一个不可变列表,您无法修改。
更具体地说,add()、addAll() 和 remove() 方法未实现。
来自 javadoc of Arrays.asList()
(强调我的):
Returns a fixed-size list backed by the specified array
简而言之:您不能从这样的列表中 .add*()
或 .remove*()
!您必须使用另一个可修改的 List 实现(例如 ArrayList
)。