在CodeReview上,我发布了一段代码,并询问了改进它的提示.我得到的是使用布尔方法来检查ArrayList是否具有偶数个索引(这是必需的).这是建议的代码:
private static boolean isEven(int number)
{
return (number & 1) == 0;
}
Run Code Online (Sandbox Code Playgroud)
由于我已经为那个特定的用户提供了很多帮助,我已经决定是时候纠缠SO社区了!我真的不明白这是如何工作的.调用该方法并将ArrayList的大小作为参数(即ArrayList具有十个元素,数字= 10).
我知道一个单独&运行数字和1的比较,但在那之后我迷路了.
我读它的方式,就是说,如果number == 0和,则返回true 1 == 0.我知道第一个不是真的,后者显然没有意义.有人可以帮帮我吗?
编辑:我应该添加代码确实有效,万一有人想知道.
我有一个集合 - 一个HashSet我想从中删除一些项目......"removals"集合中的所有项目都不在原始集合中.
我在命令行中指定"source"集的大小和"removals"集合的大小,并构建它们.源集仅包含非负整数; 删除集仅包含负整数.我测量使用System.currentTimeMillis()移除所有元素所需的时间,这不是世界上最准确的秒表,但在这种情况下绰绰有余,正如您将看到的那样.这是代码:
import java.util.*;
public class Test
{
public static void main(String[] args)
{
int sourceSize = Integer.parseInt(args[0]);
int removalsSize = Integer.parseInt(args[1]);
Set<Integer> source = new HashSet<Integer>();
Collection<Integer> removals = new ArrayList<Integer>();
for (int i = 0; i < sourceSize; i++)
{
source.add(i);
}
for (int i = 1; i <= removalsSize; i++)
{
removals.add(-i);
}
long start = System.currentTimeMillis();
source.removeAll(removals);
long end = System.currentTimeMillis();
System.out.println("Time taken: " + (end - start) + "ms");
}
}
Run Code Online (Sandbox Code Playgroud)
让我们从一个简单的工作开始: …
我的GUI出错了.尝试设置标题栏图标,然后将其包含在Runnable JAR中.
BufferedImage image = null;
try {
image = ImageIO.read(getClass().getClassLoader().getResource("resources/icon.gif"));
}
catch (IOException e) {
e.printStackTrace();
}
frame.setIconImage(image);
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误:
Exception in thread "main" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(Unknown Source)
at GUI.<init>(GUI.java:39)
at GUI.main(GUI.java:351)
Run Code Online (Sandbox Code Playgroud)
图像位于正确的目录中,"resources"文件夹是项目文件的根目录
我有一个方法,它采用可变长度字符串(String ...)作为参数.我List<String>和我在一起.我如何将此作为参数传递给方法?
我知道在Java中,静态方法就像实例方法一样继承,不同之处在于,当重新声明它们时,父实现被隐藏而不是被覆盖.好吧,这很有道理.但是,Java教程指出了这一点
接口中的静态方法永远不会被继承.
为什么?常规和接口静态方法有什么区别?
当我说静态方法可以继承时,让我澄清一下我的意思:
class Animal {
public static void identify() {
System.out.println("This is an animal");
}
}
class Cat extends Animal {}
public static void main(String[] args) {
Animal.identify();
Cat.identify(); // This compiles, even though it is not redefined in Cat.
}
Run Code Online (Sandbox Code Playgroud)
然而,
interface Animal {
public static void identify() {
System.out.println("This is an animal");
}
}
class Cat implements Animal {}
public static void main(String[] args) {
Animal.identify();
Cat.identify(); // This does not compile, because interface …Run Code Online (Sandbox Code Playgroud) 首先,这是代码:
<form action="FirstServlet" method="Post">
Last Name: <input type="text" name="lastName" size="20">
<br><br>
<input type="submit" value="FirstServlet">
<input type="submit"value="SecondServlet">
</form>
Run Code Online (Sandbox Code Playgroud)
我想了解如何在FirstServlet button被按下的情况下发送信息,以防FirstServlet万一SecondServlet button被按下SecondServlet.
重要的是:
我想以相同的形式进行,因此相同的信息将被转换为两个servlet.(当然,在servlet中我将相应地使用信息)
是否可以使用Java反射从另一个类实例化私有内部类.例如,如果我采用此代码
public class Main {
public static void main(String[] args) {}
}
class OtherClass {
private class Test {}
}
Run Code Online (Sandbox Code Playgroud)
是否可以从类main中的main方法实例化并获得对Test的访问权限.
有什么理由我更喜欢Collection.sort(list)方法而不是简单地调用list.sort()?无论如何,内部Collection.sort只是调用类的sort方法List.
令人惊讶的是,几乎每个人都在告诉我使用它Collection.sort.为什么?
class Hello12 {
static int b = 10;
static {
b = 100;
}
}
class sample {
public static void main(String args[]) {
System.out.println(Hello12.b);
}
}
Run Code Online (Sandbox Code Playgroud)
在运行上面的代码时,输出为100,因为当我调用Hello类时,首先执行静态块,将b的值设置为100并显示它.但是当我写这段代码时:
class Hello12 {
static {
b = 100;
}
static int b = 10;
}
class sample {
public static void main(String args[]) {
System.out.println(Hello12.b);
}
}
Run Code Online (Sandbox Code Playgroud)
这里的输出为10.我希望答案为100,因为一旦执行静态块,它给b的值为100.所以在main()中,我调用Hello.b它应该引用b(= 100) .两个代码中的内存如何分配给b?