我需要对以下数组进行排序:
String [][] myArray = {{"John", "3", "10"}, {"Paul", "16","2"},
{"Mike","8","20"}, {"Peter","5","0"}};
Run Code Online (Sandbox Code Playgroud)
进入以下数组:
String [][] myArray = {{"John", "3", "10"}, {"Mike","8","20"},
{"Paul", "16","2"}, {"Peter","5","0"}};
Run Code Online (Sandbox Code Playgroud)
我有两节课.第一个包含数组,另一个实现Comparator接口.这是MyArray类:
public class MyArray {
public static void main(String[] args) {
String [][] myArray = {{"John", "3", "10"}, {"Paul", "16","2"},
{"Mike","8","20"},{"Peter","5","0"}};
ByName byName = new ByName();
Arrays.sort(myArray,byName);
}
}
Run Code Online (Sandbox Code Playgroud)
这是ByName类:
import java.util.Comparator;
public class ByName implements Comparator {
public int compare(Object o1, Object o2) {
String name1 = (String)((MyArray) o1)[0]);
String name2 = (String)((MyArray) o2)[0]);
return …Run Code Online (Sandbox Code Playgroud) 嗨我正在使用下一个代码来尝试停止一个线程,但是当我看到Running为false时它再次成为现实.
public class usoos {
public static void main(String[] args) throws Exception {
start();
Thread.sleep(10000);
end();
}
public static SimpleThreads start(){
SimpleThreads id = new SimpleThreads();
id.start();
System.out.println("started.");
return id;
}
public static void end(){
System.out.println("finished.");
start().shutdown();
}
}
Run Code Online (Sandbox Code Playgroud)
和线程
public class SimpleThreads extends Thread {
volatile boolean running = true;
public SimpleThreads () {
}
public void run() {
while (running){
System.out.println("Running = " + running);
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {}
}
System.out.println("Shutting down thread" + …Run Code Online (Sandbox Code Playgroud) 我只是尝试ChronicleMap并立即遇到一个问题,"UnsupportedClassVersionError:Unsupported major.minor version 52.0".
我正在使用Java 7 ...我是否应该将此错误解释为表示Chronicle Map是在Java 8下编译的?我是否需要下载源代码并在Java 7下编译来解决此问题(这甚至可以工作)?
我正在学习使用Java学习GUI编程,我正在使用Netbeans IDE.我现在想创建一个在Netbeans之外运行的可执行程序; 即,以便它可以从我的桌面运行.
但是当我做一些研究时,我的印象是不建议从Java程序创建.exe.你能不能向我解释为什么不推荐它?
今天在java中遇到一个代码片段,如下:
long longValue = 100;
byte b = (byte)(short)(int)longValue;
byte byteValue = 100;
long l = (long)(int)(short)byteValue;
System.out.println(b+l);
Run Code Online (Sandbox Code Playgroud)
多次类型转换的目的是什么,从 int 到 short 到 byte,从 short 到 int 到 long?如果我直接从 long 转换为 byte 或反之亦然,会有什么不同吗?
即使没有显式类型转换,上面的代码也没有任何区别!
再会!我有调度程序,我需要检查它的工作时间。我怎样才能做到这一点?当调度程序工作超过 5 分钟时,我需要从 someFunction() 返回错误
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
public int someFunction() {
ScheduledFuture<Integer> future =
scheduler.schedule(new ScheduledPrinter(), 10, TimeUnit.SECONDS);
if (scheduler.isShutdown()) {
return future.get();
}
}
private class ScheduledPrinter implements Callable<Integer> {
public Integer call() throws Exception {
// do something
if (!serverResponse.getErrorData().equals("QueueIsNotReady")) {
scheduler.shutdown();
return getResponse();
}
return null;
}
}
Run Code Online (Sandbox Code Playgroud) 考虑这个函数:
import java.io.IOException;
import java.io.Reader;
class StreamPrinter {
void print(Reader reader) throws IOException {
int code = reader.read();
while (code != -1) {
System.out.print((char) code);
code = reader.read();
}
reader.close();
}
}
Run Code Online (Sandbox Code Playgroud)
如何在异常和错误的情况下关闭流?
import java.util.Random;
class perro{
private Float animo;
private Float umbral;
Random rand = new Random();
public perro(){
animo = rand.nextFloat() * (5+5)-5;
umbral = rand.nextFloat() * (0-20);
}
public Float getAnimo(){
return animo;
}
public Float getUmbral(){
return umbral;
}
private void psican(persona p){
this.animo = (rand.nextFloat() -0.5) * 10 + this.animo;
if (p.tieneGalleta()){
p.darGalleta();
this.animo += (1.0 / p.getConfiabilidad()) * (rand.nextInt(2));
}
System.out.println("Mi nuevo animo es: " + this.animo);
}
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
"error: incompatible types: double cannot be converted to …Run Code Online (Sandbox Code Playgroud) 对于关注其发展的人们来说,这并不是一个大秘密,Shenandoah一个主要的批评是它GC barriers用于每一次写入和读取:无论是引用还是原始。
Shenandoah 2.0声称这不再是问题,可以通过所谓的加载引用屏障解决。这是怎么发生的?
例子:
class A {
public static Integer age;
}
// Approach 1:
class Exe1 {
public void exec() {
synchronized(A.class) {
//do something
}
}
}
// Approach 2
public class Exec2 {
public void exec() {
synchronized(A.age) {
//do something
}
}
}
Run Code Online (Sandbox Code Playgroud)
方法 2 中的代码与方法 1 的行为方式相同吗?
我的意思是,这两种方法在控制代码执行顺序方面具有相同的效果吗?