以下是抛出compile error:
int[] arrs = {1,2,4,3,5,6};
List<Integer> arry = Arrays.asList(arrs);
Run Code Online (Sandbox Code Playgroud)
但这有效:
for (Integer i : arrs){
//do something
}
Run Code Online (Sandbox Code Playgroud)
自动拳击在许多情况下都有效,我刚才给出了一个例子for-loop.但是List-view我做的却失败了Arrays.asList().
为什么这会失败?为什么选择设计实现?
这个问题更多的是Java开发人员的设计实现.我想知道(如果有任何重要的原因我无法想到)为什么Keyset()返回一个set-view但values()返回Collection-view.为什么不回到Values()一个ValueSet与set-view.如果需要,我可以进行设置,但为什么选择它的方式.
也许这有助于决定在构建自定义数据时使用哪些数据结构.
Map<String, Integer> map = new HashMap<String,Integer>();
map.put("hello",1);
map.put("world",2);
Collection <Integer> i = map.values();
Set<String> s = map.keySet();
Run Code Online (Sandbox Code Playgroud) 我一直在寻找这些问题的答案.但没有太多运气.
是否可以32-bit在机器中运行代码64-bit processor?
答案似乎是肯定的.但是在性能问题上存在争议,因为32-bits处理器上没有使用它们.
现在我的问题反之亦然,是否可以64-bit在带32-bit处理器的机器中运行代码?
根据我的理解,答案是否定的,因为设计用于运行的代码64-bit
将使用,64-process registars但32-bit机器仅提供32.
另一方面,我找到了这个链接.据此,可以在32位机器上编译64位代码.但我不知道如何做到这一点加上如果在32-bit机器上进行编译也能保证execution相同.
谢谢你的帮助
这是我在这里发布的问题的扩展,虽然这似乎解决了我的问题的一部分,现在我明白了IO-Exception read end dead exception.我正在使用一个multithreaded应用程序,其中 thread-1 produces随机数和其他thread-2 consumes它来计算平均值.一旦平均值达到阈值,我发出信号thread-1停止产生数字.这是代码的基本设计.
我到了IO-Exception read end dead exception.我想知道为什么会这样,以及如何解决它.谢谢.
代码如下:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.util.Random;
import java.util.concurrent.atomic.AtomicBoolean;
//
class NumGen extends Thread {
PipedOutputStream pos;
DataOutputStream dos;
AtomicBoolean isDone;
public NumGen(PipedOutputStream pos,AtomicBoolean isDone){
this.pos=pos;
dos=new DataOutputStream(pos);
this.isDone=isDone;
}
public void run(){
while (!isDone.get()){
Random rand = new Random();
try {
dos.writeDouble(rand.nextDouble()+100.0);
} catch (IOException e) { …Run Code Online (Sandbox Code Playgroud) 在下面的代码中,构造函数的Child可见性降低public了private,这是允许的.继承的方法(例如)test()不能降低可见性.为什么Java以这种方式运行?
class Parent {
public Parent(){}
public void test()
{
System.out.print("parent test executed!");
}
}
class Child extends Parent{
private Child(){}
private void test(){
System.out.print("child test executed!");
}
}
Run Code Online (Sandbox Code Playgroud) 我想通过发送用户activation email点击来激活用户.我想它目前尚未合并Django 1.6.用 Django编码的用户注册应用似乎就是为了这个目的.但是我对forms.py中DefaultForm提供的内容有些怀疑.我想要包含更多字段.如何在那里实现它.如果我安装这个应用程序,更改直接包含更多字段是一个好主意,有没有更好的方法来实现相同.class RegistrationForm(forms.Form)
在views.py中,我看到一些方法如下未实现.我不清楚这些方法需要做什么.我应该将网址重定向到这些页面吗?
def register(self, request, **cleaned_data):
raise NotImplementedError
def activate(self, request, *args, **kwargs):
raise NotImplementedError
def get_success_url(self, request, user):
raise NotImplementedError
Run Code Online (Sandbox Code Playgroud) 我收到以下错误:
FieldError at /blog/1/first-post/
Cannot resolve keyword u'slug' into field. Choices are: article, date, id, likes
Request Method: GET
Request URL: http://127.0.0.1:8000/blog/1/first-post/
Django Version: 1.6.2
Exception Type: FieldError
Exception Value:
Cannot resolve keyword u'slug' into field. Choices are: article, date, id, likes
Run Code Online (Sandbox Code Playgroud)
我的模特:
class Article(models.Model):
title = models.CharField(max_length=20)
body = models.TextField()
image = models.ImageField(upload_to="/", blank=True, null=True)
slug = models.SlugField()
def save(self, *args, **kwargs):
if not self.id:
self.slug = slugify(self.title)
super(Article, self).save(*args, **kwargs)
def get_absolute_url(self):
return reverse('article_detail', kwargs={'slug':self.slug, 'id':self.id})
def __unicode__(self): …Run Code Online (Sandbox Code Playgroud) 我正在阅读oracle docs中提供的Jersey Sample中的存储服务示例示例.
我只是无法理解JAX-RS运行时如何解决这个PUT请求?
curl -X PUT http://127.0.0.1:9998/storage/containers/quotes
Run Code Online (Sandbox Code Playgroud)
这是与此请求对应的代码段(取自上面的链接).
@Path("/containers")
@Produces("application/xml")
public class ContainersResource {
@Context UriInfo uriInfo;
@Context Request request;
@Path("{container}")
public ContainerResource getContainerResource(@PathParam("container") String container) {
return new ContainerResource(uriInfo, request, container);
}
@GET
public Containers getContainers() {
System.out.println("GET CONTAINERS");
return MemoryStore.MS.getContainers();
}
}
Run Code Online (Sandbox Code Playgroud)
但是你可以注意到,没有办法@PUT annotation.但是需要这种getContainerResource方法/containers/{container}.在此方法中,ContainerResource返回一个新实例.我不确定如何处理上述PUT请求.请解释.
这是ContainerResource class:
@Produces("application/xml")
public class ContainerResource {
@Context UriInfo uriInfo;
@Context Request request;
String container;
ContainerResource(UriInfo uriInfo, Request …Run Code Online (Sandbox Code Playgroud) 我想拦截任何class或methods注释为@Foo
类级别拦截:
@Foo
@path("/foo")
public class Attack {...}
Run Code Online (Sandbox Code Playgroud)
方法级拦截:
@path("/bar")
public class defend {
@Foo
@GET
public String myMethod(){....}
Run Code Online (Sandbox Code Playgroud)
我想拦截任何带有注释的类或方法,@Foo但不拦截其他方法或类。我想在继续方法执行之前打印出整个路径或 URI。一方法调用完成,我想打印出“执行成功”
这是这样的事情:
system.out.println(path) // this is the path the request is made. something like /api/2/imp/foo
method call happens
method call finishes
System.out.println("executed successfully")
Run Code Online (Sandbox Code Playgroud)
我的情况有所不同,但这是我遇到的根本问题。我不想具体实施。Java EE 7 规范有一种方法可以使用 @Postconstruct、@AroundInvoke 等来做到这一点。但我真的很难组装它。
这篇文章绝对是解决这个问题的好方法。但它是特定于实现的(RESTeasy)并且AcceptByMethod它使用的已被弃用。
谢谢
这是我试过的.它甚至没有编译.
public class LambdaExample {
public static Integer handleOperation(Integer x, Integer y, Function converter){
return converter.apply(x,y);
}
public static void main(String[] args){
handleOperation(10,10, Operation::add);
}
}
class Operation {
public int add(Integer x, Integer y){
return x+y;
}
}
Run Code Online (Sandbox Code Playgroud)
我想在这里努力实现的一些事情是:
1)如何传递lambda expression方法参数(在main上面的方法中)
2)如何将参数传递给函数(在 handleOpertion方法中,存在compilation应用的错误只需要一个参数)
java ×5
django ×2
django-views ×2
jax-rs ×2
32-bit ×1
arrays ×1
autoboxing ×1
c ×1
collections ×1
compilation ×1
constructor ×1
copy ×1
django-forms ×1
django-urls ×1
hashmap ×1
inheritance ×1
interceptor ×1
ioexception ×1
java-8 ×1
java-ee ×1
java-ee-7 ×1
jersey ×1
keyset ×1
lambda ×1
list ×1
map ×1
methods ×1
processor ×1
put ×1