给出一个n个不同数字的未排序数组作为输入,其中n是2的幂.给出一个算法,该算法识别数组中第二大的数字,并且最多使用n + log 2(n)-2比较.
在做简单的程序时我注意到了这个问题.
int[] example = new int[10];
List<Integer> exampleList = Arrays.asList(example);// Compilation error here
Run Code Online (Sandbox Code Playgroud)
编译错误返回为cannot convert from List<int[]> to List<Integer>.但是List<int>java中是不允许的,为什么会出现这种编译错误?
我不是在质疑autoboxing在这里我只是想知道如何Arrays.asList返回List<int[]>.
asList实现是
public static <T> List<T> asList(T... a) {
return new ArrayList<T>(a);
}
Run Code Online (Sandbox Code Playgroud)
所以它将int []视为T就是为什么会发生这种情况.
Java提供了使用定义对象外部对象的比较的方法Comparator.
现在我的问题是为什么java不允许对equals()和hashcode()做同样的事情.
现在每个集合contains()方法都可以轻松使用此外部相等提供程序来检查对象是否相等.
我已经评估了几个开源文档管理系统,似乎这些在过去,例如opendocman,quotero
我正在寻找一个开源文档管理系统,它提供了以下功能.
如果您有使用任何人的经验并且我可以使用它,请告诉我?
JLS 2.13.1接口修饰符
接口不能是最终的,因为这样的类的实现永远无法完成.
如果我可以在接口I中编写创建静态内部类,可以在其中提供实现,那么为什么会有这样的限制
interface Type {
// Normal
class Value {
private Value() {
}
public void print() {
System.out.println("Test");
}
}
public final Value value = new Value();
}
Run Code Online (Sandbox Code Playgroud) 为什么T extends String允许但是发出警告?
类型参数T不应受最终类型String的限制.最终类型无法进一步扩展
public class Car<T extends String>
Run Code Online (Sandbox Code Playgroud)
我知道什么是最终我知道它是有效的,因为只有可能的价值T can be String我想知道警告.
为这个问题做例子
我注意到intern()方法的一个奇怪的行为,当我调用intern()方法String后,我可以使用==运算符的原始字符串.
JavaDoc的intern()方法:
返回字符串对象的规范表示.最初为空的字符串池由String类私有维护.
当调用intern方法时,如果池已经包含与该
equals(Object)方法确定的String对象相等的字符串,则返回池中的字符串.否则,将此String对象添加到池中,并返回对此String对象的引用.它遵循对于任何两个字符串
s和t,s.intern() == t.intern()为真,当且仅当s.equals(t)是真实的.
上面的Javadoc并没有说原始字符串会发生变化.那么为什么这个程序打印okay时test输入.
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner user_input = new Scanner(System.in);
String username;
System.out.print("username: ");
username = user_input.next();
// Even if I do not assign returned string for comparison still it …Run Code Online (Sandbox Code Playgroud) 在下面的程序中,我不明白为什么有ClassCastExceptionfor cast fromint.class
更新:
我应该指定我知道什么是原始类型。What I don't understand is why int.class is provided with broken implementation?
public static void main(String[] args) {
System.out.println(DataType.INT.getValue(Integer.class));
System.out.println(DataType.INT.getValue(int.class));//Class cast exception here
}
enum DataType {
INT {
@Override
public <T> T getValue(Class<T> toClass) {
return toClass.cast(1000);//ClassCastException here for int.class
}
};
public abstract <T> T getValue(Class<T> toClass);
}
Run Code Online (Sandbox Code Playgroud) toArray方法隐藏<E>传递给Collection<E>接口.以下是方法签名.
<T> T[] toArray(T[] a);
Run Code Online (Sandbox Code Playgroud)
因为以下是可能的.并将结果转化为ArrayStoreException
ArrayList<String> string = new ArrayList<String>();
string.add("1");
string.add("2");
Integer intArray[] = new Integer[2];
intArray = string.toArray(intArray);
Run Code Online (Sandbox Code Playgroud)
我想知道为什么会做出这样的决定?为什么在设计API时允许这样的情况?无论如何这个代码导致了RuntimeException?
我想导入每天导出到任何开源错误跟踪器的内容XML。JIRA我用谷歌搜索了它,但我找不到任何能指引我正确方向的东西。
如果您知道任何可以导入从 JIRA 导出的错误的开源错误跟踪器,您能帮我吗?
我正在做一个示例练习,我发现一个奇怪的观察,如果我用挥发性程序替换AutomicInteger运行得更快.注意:我只做读操作.
码:
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
public class Main {
AtomicInteger integer = new AtomicInteger(100000000);
// volatile int integer= 100000000;
public static void main(String[] args) {
// We will store the threads so that we can check if they are done
List<Thread> threads = new ArrayList<Thread>();
long start = System.currentTimeMillis();
Main main = new Main();
// We will create 500 threads
for (int i = 0; i < 500; i++) {
Runnable task = new MyRunnable(main.integer);
Thread worker …Run Code Online (Sandbox Code Playgroud) clone()方法在Object类中默认不可见,那么它如何不为Array类型提供错误?
这是否意味着有一个名为int []的类型,其实现是用java编写的,如果是,在哪里找到它?
如果是写的那么为什么不写完呢?
我的意思是为什么不正确地实现每个方法而不仅仅是来自ObjectClass 的行为.
int[] a ={1,2,3};
Object object = new Object();
object.clone();//Does not compile since clone is protected.
a.clone();// allowed as this method is from int[]
Run Code Online (Sandbox Code Playgroud) 此问题基于 Synchronizing on Integer值.
解决方案似乎很好只有小问题它没有解决关于如何从中删除值的问题ConcurrentHashMap.
所以要解决我在下面的程序
import java.util.concurrent.ConcurrentHashMap;
public class Example {
private final ConcurrentHashMap<Integer, Integer> concurrentHashMap = new ConcurrentHashMap<Integer, Integer>();
public void doSomething(int i) {
synchronized (getLockForId(i)) {
concurrentHashMap.remove(i);
}
}
public Integer getLockForId(int id) {
concurrentHashMap.putIfAbsent(id, id); // I want to replace these two
// operation with single one
// since it seems the cause of
// NPE
return concurrentHashMap.get(id);
}
public static void main(String[] args) {
final Example example = new Example();
new Thread(new …Run Code Online (Sandbox Code Playgroud) java ×11
generics ×3
algorithm ×1
bug-tracking ×1
clone ×1
collections ×1
concurrency ×1
dms ×1
equality ×1
interface ×1
jira ×1
open-source ×1
string ×1