在我的代码中,我有许多传递给排序方法的ArrayLists.这些ArrayLists中的每一个都具有不同的Generic类型,但所有这些类型都是Sorter的实现.排序方法旨在接受Sorter类型的ArrayLists.我的问题是我还没有找到一种方法将arraylists的类型转换为超类型,以便将它们传递给sorter方法.以下是分拣机方法的格式:
public static ArrayList<Sorter> quicksort(ArrayList<Sorter> members);
Run Code Online (Sandbox Code Playgroud)
Spatial类是分拣机的实现,如下所示:
public abstract class Spatial implements Sorter
Run Code Online (Sandbox Code Playgroud)
但是,当调用quicksort方法时会生成错误(children是Spatial类型的ArrayList):
children = ListSorter.quicksort(children);
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我有一个自定义对象,其结构是
public String name;
public int type;
public String createdOn;
Run Code Online (Sandbox Code Playgroud)
createdOn 是日期,其格式是: 2011-12-16T23:27:27+0000
我已经存储了多个这种类型的对象,ArrayList现在我想根据创建日期和时间对它们进行排序.
我尝试过使用Collections.sort(....),但没有合适的结果.
我试着保持这个简单,但仍然可以理解.如果有任何困惑,请告诉我!
我得到了这个哈希表:
private Hashtable<String, ArrayList<String>> allChannels = new Hashtable<String, ArrayList<String>>();
Run Code Online (Sandbox Code Playgroud)
我正在使用此方法插入客户端/用户:
public void connectChannel(String username, String channel) throws RemoteException{
allChannels.put(channel, new ArrayList<String>());
allChannels.get(channel).add(username);
}
Run Code Online (Sandbox Code Playgroud)
现在我想使用这个方法,以获得一个连接到给定通道(参数)的用户的arraylist.怎么可以这样做?
@Override
public ArrayList<String> getUsersInChannel(String channel) throws RemoteException{
return **Code needed**
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
编辑:顺便说一句,连接方法得到了几个检查,但它很长,所以我没有打扰它,因为我知道它的工作很好.
编辑:因为使用return allChannels.get(channel)似乎是正确的,所以我的JList还有其他问题.我在GUILogic中使用了这些方法来运行JList.我已经使另外两个JList以相同的方式工作了.
DefaultListModel usersInChanDLM = new DefaultListModel();
public DefaultListModel getUsersInChannelAsDefaultListModel() throws RemoteException{
if(!(getChannel() == null)){
for(String a : cf.getUsersInChannel(getChannel())){
usersInChanDLM.addElement(a);
System.out.println(a);
}
}
return usersInChanDLM;
}
void updateUsersInChannelJlist(JList jList3) throws RemoteException {
usersInChanDLM.clear();
usersInChanDLM.removeAllElements();
for(Client c : cf.getClients()){
if(!(usersInChanDLM.contains(c.findName()))){
usersInChanDLM.addElement(c.findName());
}
} …Run Code Online (Sandbox Code Playgroud) 什么是运行时间lastIndexOf为ArrayLists在Java中?
我无法弄清楚为什么我不能将项添加到ArrayList.我尝试了几种不同的方式添加项目,但它们不起作用.
class Problem {
public ArrayList<String> problems = new ArrayList<String>();
public ArrayList<String> answers = new ArrayList<String>();
private String question1 = "What is 2+2?";
private String question2 = "What is the square root of 25";
private String question3 = "What is the next number in the sequence? {2, 4, 6}";
private String question4 = "What is 8*8?";
String[] temp1 = {question1, question2, question3, question4};
for (String s : temp1)
problems.add(s);
}
Run Code Online (Sandbox Code Playgroud)
我也试过了
problems.add(question1);
problems.add(question2);
problems.add(question3);
problems.add(question4);
Run Code Online (Sandbox Code Playgroud)
这也不起作用.
编译器说标识符是预期的.
我有两个arraylists,由datetime对象组成,并使用合并列表.
list1.addAll(list2);
Run Code Online (Sandbox Code Playgroud)
我想现在按照arraylist所包含的日期时间对象的顺序对这个arraylist进行排序.任何人都可以指导我如何排序这个arraylist.
这是这个问题的后续问题:通过扫描仪将1行txt文件分配给字符串 我已经实现了ArrayList,这样如果提供了答案,程序应该睡眠10,000毫秒(否则它会要求你"查找" "(注意a.txt的格式是
然而,当我运行该程序时,即使提供了答案,它仍然会要求您"查找"而不是睡觉,我很确定这与如何定位问题/答案有关.继承我的代码,Eclipse显示没有错误 -
public class Test {
public static void main(String[] args) throws InterruptedException {
// Location of file to read
File file = new File("a.txt");
List<String> lines = new ArrayList<String>();
try {
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
lines.add(scanner.nextLine());
}
scanner.close();
for (int i = 1 ; i < lines.size(); i+=2)
{
String question = lines.get(i - 1);
String answer = lines.get(i += 1);
String a = JOptionPane.showInputDialog("" + question); …Run Code Online (Sandbox Code Playgroud) 是否可以在数组列表中包含多个数据类型,或者甚至是两个相同的数据类型,例如:
ArrayList<Integer,Integer,Integer> triple = new ArrayList<Integer,Integer,Integer>();
Run Code Online (Sandbox Code Playgroud)
我注意到这会导致错误,但我只是想知道是否确实有办法做到这一点?
我正在编写一个程序来列出字母A,B,C和D的所有可能组合.我已经成功编写了一个程序来列出所有可能的排列.
但是,如何重新编写程序并生成所有组合(即:ABCD = DCBA和AB = BA,因此只要有一个,另一个不需要列出).
到目前为止,我当前程序的代码是:
import java.util.ArrayList;
public class Perms {
public static void main(String[] args) {
ArrayList<Character> characters = new ArrayList<Character>();
characters.add('A');
characters.add('B');
characters.add('C');
characters.add('D');
int count = 0;
for (int i = 0; i < characters.size(); i++) {
for (int j = 0; j < characters.size(); j++) {
for (int k = 0; k < characters.size(); k++) {
for (int d = 0; d < characters.size(); d++) {
count++;
System.out.println(count + ": " + characters.get(i) + …Run Code Online (Sandbox Code Playgroud) 例如,假设我有一个ArrayList可能包含以下值的值:
x
x
x
y
y
Run Code Online (Sandbox Code Playgroud)
现在我想要检索的是和的数量,x并且x我希望能够区分我拥有的,x或者y因为实际上,我可以在ArrayList中有任何对象,我必须能够区分它们.
我想要做的是首先将其转换ArrayList为a LinkedHashSet,这将保持排序并删除重复项,因此我只会拥有x,y但是我如何得到每个的数量并将其与正确的元素相关联?
总的来说,我想要做的是能够编写一个toString让我输出的方法:
x3y2
Run Code Online (Sandbox Code Playgroud)
但是,如果没有知道x和y是元素,因为他们本来是别的东西像z或w.