java - java中如何将数组作为参数?

标签 java arrays object

我尝试使用数组作为参数,但是当尝试将数组作为参数时,出现以下错误:

required: int,int[] found: int,int reason: actual argument int cannot be converted to int[] by method invocation conversion 2 errors

源代码:

public class utopian{

    void height(int t,int[] n){

    n = new int[100];
    for (int i=0;i<t;i++){
        int k = n[i];
        if (k%2==0){
            for(int j=0;j<=k;j=j+2){
                int h=1;
                h=h*2;
                h=h+1;

            }}
            else{
                for(int j=0;j<=k;j=j+2){
                int h=1;
                h=h*2;
                h=h+1;

            }
           int h=h*2;    
            }
    System.out.println(h);  
            }        
    }

}

--

public class Solution {

    public static void main(String[] args) {
        utopian heht = new utopian();
        heht.height(2,{0,1})
}}

我想使用数组作为参数,但我不明白如何使用。

最佳答案

heht.height(2, new int[] {0,1})

但是,有些地方可以使用不太详细的符号,这可能会误导您:

int[] data = {0, 1};

关于java - java中如何将数组作为参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26216958/

相关文章:

java - Jersey ModelValidationException - 未找到注入(inject)源

JAVA:InputStream.read中的字节数组分配(byte[] b, int off, int len)

java - 如何从Java中的构造函数反序列化对象?

java - 如何在 Java 中使用对象属性填充 JTable?

java - 无法在 Camel 中使用 jaxb 解码。不调用处理器

java - 在 Java 中调用 Oracle Sproc 时出现问题(使用 Spring 3.0)

java - 计算矩阵中元素之间的路径数

javascript - 匹配相同值的十进制数以插入数组

c++ - 编译存储在外部文本文件中的数组(使用命令行g++编译的C++)

java - 从文本文件输入创建对象