小编bra*_*orm的帖子

为什么整数的自动装箱和拆箱不能与Java中的Arrays.asList一起使用?

以下是抛出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 arrays autoboxing copy list

4
推荐指数
1
解决办法
442
查看次数

为什么map.keyset()返回set视图但map.values()返回Java中的集合?

这个问题更多的是Java开发人员的设计实现.我想知道(如果有任何重要的原因我无法想到)为什么Keyset()返回一个set-view但values()返回Collection-view.为什么不回到Values()一个ValueSetset-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)

java collections hashmap map keyset

4
推荐指数
1
解决办法
1135
查看次数

是否可以在32位处理器的机器中运行64位代码?

我一直在寻找这些问题的答案.但没有太多运气.

  1. 是否可以32-bit在机器中运行代码64-bit processor

    答案似乎是肯定的.但是在性能问题上存在争议,因为32-bits处理器上没有使用它们.

  2. 现在我的问题反之亦然,是否可以64-bit在带32-bit处理器的机器中运行代码?

    根据我的理解,答案是否定的,因为设计用于运行的代码64-bit 将使用,64-process registars32-bit机器仅提供32.

另一方面,我找到了这个链接.据此,可以在32位机器上编译64位代码.但我不知道如何做到这一点加上如果在32-bit机器上进行编译也能保证execution相同.

谢谢你的帮助

c 32-bit compilation processor cpu-registers

4
推荐指数
1
解决办法
1047
查看次数

IO异常 - 读取结束死 - 在此示例中导致它的原因以及如何修复它 - Java中的多线程应用程序

这是我在这里发布的问题的扩展,虽然这似乎解决了我的问题的一部分,现在我明白了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)

java multithreading ioexception

4
推荐指数
1
解决办法
2652
查看次数

Java继承:降低构造函数与继承方法的可见性

在下面的代码中,构造函数的Child可见性降低publicprivate,这是允许的.继承的方法(例如)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)

java methods inheritance constructor access-modifiers

4
推荐指数
1
解决办法
700
查看次数

django用户注册和电子邮件认证

我想通过发送用户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)

django django-forms django-views user-registration

4
推荐指数
1
解决办法
9978
查看次数

无法在Django中将关键字u'slug'解析为字段错误?

我收到以下错误:

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)

django django-models django-urls django-views

4
推荐指数
2
解决办法
7357
查看次数

在这个例子中,使用子资源的PUT请求是如何由JAX-RS运行时处理的?

我正在阅读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)

jax-rs put jersey java-ee

4
推荐指数
1
解决办法
1211
查看次数

如何拦截 Java EE 7 兼容容器中 JAX-RS 中的选择性方法和类?

我想拦截任何classmethods注释为@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它使用的已被弃用。

谢谢

java jax-rs interceptor java-ee-7

4
推荐指数
1
解决办法
5345
查看次数

如何在Java 8中将带参数的lambda表达式作为参数传递?

这是我试过的.它甚至没有编译.

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应用的错误只需要一个参数)

lambda java-8 functional-interface

4
推荐指数
1
解决办法
4294
查看次数