相关疑难解决方法(0)

从数组创建ArrayList

我有一个初始化的数组,如:

Element[] array = {new Element(1), new Element(2), new Element(3)};
Run Code Online (Sandbox Code Playgroud)

我想将此数组转换为ArrayList类的对象.

ArrayList<Element> arraylist = ???;
Run Code Online (Sandbox Code Playgroud)

java arrays arraylist type-conversion

3441
推荐指数
31
解决办法
144万
查看次数

为什么indexOf无法找到对象?

我创建了一个整数列表,并尝试返回特定值的索引.数组是3,8,2,5,1,4,7,6,我想返回indexOf(3),它应该是0.

在导入java.util之后,我在Eclipse Java Scrapbook中尝试了以下内容.*:

int[] A = {3,8,2,5,1,4,7,9};
Arrays.asList(A).indexOf(3)
Run Code Online (Sandbox Code Playgroud)

我也尝试过:

int[] A = {3,8,2,5,1,4,7,6};
ArrayList<Integer> l = new ArrayList(Arrays.asList(A));
l.indexOf(3)
Run Code Online (Sandbox Code Playgroud)

两者都返回-1.为什么?如何让它按预期工作?

java indexof

32
推荐指数
2
解决办法
1万
查看次数

使用Java输出或返回,而不使用字母e或数字5

对于PCG.SE上的这一挑战,需要编写一个返回/输出前N个素数的函数/程序.

但是,程序中不允许使用带有主要代码点的字符.

其中两个字符是5e.两个字母的代码点都包含一个5.因此,\u{codepoint}这两封信不会起作用.

使用转义,我可以从代码中删除所有主要字符,除了ein return.

我可以使用System.out或者FileDescriptor.out都包含这封信e.

有没有办法返回或输出而不使用e5

这里的参考是我的代码与字符转义:

int[]b(int b\u0029{
    int c,d,f[]\u003d{}\u003b
    for(f\u003dj\u0061v\u0061.util.Arr\u0061ys.copy\u004ff(f,b\u0029\u003bb-->0\u003b\u0029
        for(d\u003d0,c\u003d2\u003bf[b]<1\u003bf[b]\u003dd<1?c:f[b],d\u003d0,c\u002b\u002b\u0029
            for(int h:f\u0029
                d\u003dh>0&&c\u002fh*h\u003d\u003dc?1:d\u003b
    return f;
}
Run Code Online (Sandbox Code Playgroud)

没有字符转义:

int[]b(int b){
    int c,d,f[]={};
    for(f=java.util.Arrays.copyOf(f,b);b-->0;)
        for(d=0,c=2;f[b]<1;f[b]=d<1?c:f[b],d=0,c++)
            for(int h:f)
                d=h>0&&c/h*h==c?1:d;
    return f;
}
Run Code Online (Sandbox Code Playgroud)

java escaping

13
推荐指数
1
解决办法
429
查看次数

麻烦初始化List <Object []>使用Arrays.asList

当我初始化List时,我能够这样做:

List<Object[]> foo = new ArrayList<>();
foo.add(new Object[]{816, "foo", 2.6});
Run Code Online (Sandbox Code Playgroud)

但是,当我想使用Arrays.asList以下方法简化它时:

List<Object[]> bar = Arrays.asList(new Object[]{"bar", 286});
Run Code Online (Sandbox Code Playgroud)

它无法编译错误:

incompatible types: inference variable T has incompatible bounds
equality constraints: java.lang.Object[]
lower bounds: java.lang.Object
Run Code Online (Sandbox Code Playgroud)

为什么它不能做类型推断权以及如何解决这个问题?

java generics list

13
推荐指数
3
解决办法
2040
查看次数

检查数组是否有某个字符,为什么我的代码不起作用?

你好这里是一些检查有效字符的代码,虽然代码不起作用.即使'operand'是有效字符,它也永远不会打印是,并返回false.无论什么即使角色有效,它也只是不识别它并且总是转到else语句.请帮忙!

public static boolean checkValidOperands(char operand) {

    char[] validOperators = {'+', '-', '*', '/', 'q'};
    List<char[]> validOp = Arrays.asList(validOperators);
    if (validOp.contains(operand))  {
        System.out.println("Yes");
        return false;
    }  else {
        System.out.println("Please enter valid operand");
        return true;
    }
}
Run Code Online (Sandbox Code Playgroud)

java arrays search contains

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

如何将int []转换为ArrayList

我想知道如何转换int[]Integer[]Java中.

基本上我想尝试转换int[]ArrayListjava.

我找到了一些例子,但它们是基于Integer对象而不是基于int[].

refer: 将int []转换为ArrayList

谢谢

java

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