有人能告诉我以下代码有什么问题吗?
Set<String> cmds = *a method call that returns a Set<String>*
String[] cmdarr = (String[]) cmds.toArray();
int i;
for(i=0; i<cmdarr.length;i++){
System.out.println(cmdarr[i]);
Run Code Online (Sandbox Code Playgroud)
它给出了以下错误:
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;
at a.jim.Test.main(Test.java:79)
Run Code Online (Sandbox Code Playgroud)
提前致谢!
根据这篇文章,我尝试定义一个新的比较器来对一个持有自定义类的arraylist进行排序.我的代码如下:
public class mainClass{
public class match {
/*I defined a new class to hold some data points*/
public int x;
public int y;
public int score;
public match(){
/*initialize values of match if needed*/
}
}
public ArrayList<match> matchList=new ArrayList<match>(); //the arraylist that holds data
/*code that fills the list with matches*/
/*the following method is supposed to sort the matchlist. a match with higher score is bigger, and with the same score, a match with bigger …Run Code Online (Sandbox Code Playgroud) 我有一些代码可能返回单个int或列表.当它返回一个int时,我需要将它转换为只包含int的列表.
我尝试了以下,但它不起作用:
newValue = list(retValue)
Run Code Online (Sandbox Code Playgroud)
显然我做不到,list(some int)因为整数不可迭代.还有另一种方法吗?
提前致谢!
我是java和makefile的新手,我想了解一些关于makefile的介绍性信息.通常在网上找到的信息大多是示例和高级别的.以下是我对makefile的一些具体/天真的问题:
什么类型的文件是makefile的目标?.class和.java?
makefile对那些项目文件做了什么?我在Eclipse中完成了我的项目,因此.class文件存储在bin中,.java文件存储在scr中.makefile在这些文件之间建立了什么样的连接?
makefile的结果是什么?makefile的目的是告诉编译器如何生成可执行文件,因此结果是当前目录中的新可执行文件,是吗?
我应该在哪里放置makefile?在bin文件夹中?scr文件夹?将所有苍蝇混合在一个目录下并放在那里?
最后,我为我的项目制作了一个小的makefile,它应该编译3个类.它不起作用,我不知道为什么:
生成文件:
JFLAGS = -g
JC = javac
.SUFFIXES: .java .class
.java.class:
$(JC) $(JFLAGS) $*.java
CLASSES = \
DNA.java \
C.java \
fastmatch.java
default: classes
classes: $(CLASSES:.java=.class)
clean:
$(RM) *.class
Run Code Online (Sandbox Code Playgroud)
我做错什么了吗?我只需将所有内容放在一个文件夹下,然后在命令行中输入"make".我想我在这里错过了一些大图,所以如果有人能向我解释,我真的很感激.谢谢
给定2个数组,如果它们相互之间的价值相同,怎么能快速找到?
例如,arr1和arr2被认为是相同的,因为它们包含相等的值,而arr2和arr3不是
int[] arr1 = new int[]{-1, 0, 1};
int[] arr2 = new int[] {-1, 0, 1};
int[] arr3 = new int[] {0, -1, 1}; // not identical
Run Code Online (Sandbox Code Playgroud)
找出最快的方法是什么?我知道for循环可以工作,但是你可以做得更快,比如恒定时间吗?HashSet不起作用,因为技术上arr1和arr2是不同的对象
编辑1:如果有N个数组,我们想要过滤出独特的数组怎么办?
我需要一段代码来设置几个并发运行的线程,在它们全部完成之后,打印一些东西来通知用户.这是代码(在Windows上使用c ++ 11的库):
void func1(){
int j = 0;
while(j<=100000) j++;
}
int main(){
for(int i = 0; i < 5; i++){
std::thread t(func1);
printf("releasing thread %d\n",i);
t.join();
printf("all threads finished\n");
}
}
Run Code Online (Sandbox Code Playgroud)
我认为这将使所有线程同时运行,但结果是它一次只运行一个线程,等到它完成,然后执行下一个线程.Outout:
releasing thread 0
releasing thread 1
releasing thread 2
releasing thread 3
releasing thread 4
all threads finished
Run Code Online (Sandbox Code Playgroud)
在这种情况下,如果不分离线程,有什么意义的join()?我该怎么做才能实现我所描述的目标?