如何访问在阵列的方法的参数? Java的 [英] How to access parameters in a method for an array? Java

查看:87
本文介绍了如何访问在阵列的方法的参数? Java的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想了解如何这应该工作的基本知识。这里是我的code .--------------------------->这是我的主类。

Just trying to understand the basics of how this should work. Here is my code.---------------------------> This is my main class.

public class Driver
{
    public static void main(String[] args)
    {
        //create new instance of the ArrayLab class with parameter of 10
        ArrayLab array = new ArrayLab(10);

        //search for 2
        array.search(2);
    }
}

类ArrayLab已分配的方法,以它调用的参数搜索(2)。到目前为止,这是我。

The class ArrayLab has a method assigned to it called search with parameter of (2). So far this is what I have.

import java.util.Arrays;
public class ArrayLab
{
    //array instance variable
    int[] array1 = new int[10];

    //array constructor
    public ArrayLab(int integer)
    {
        //class parameter = 10
        int[] array1 = new int[integer];

    }

//method
public void search(int integer)
    {
        int[] array1= new int[]{integer};
        System.out.println(Arrays.toString(array1));
    }
}

所以,最大的问题是我在做什么吗?还是错?我意识到这可能是pretty基础,只是在努力理解什么是code内部发生的情况。感谢:)

So the big question is what am I doing right? or wrong? I realize this is probably pretty basic, just struggling to understand what is happening inside the code. Thanks :)

推荐答案

驱动程序类是不错的。

所以,让我们一行在时间

So, lets take one line at a time

int[] array1 = new int[10];

好吧,你让大小10的公开int数组,更precisely [0,0,0,0,0,0,0,0,0,0]

public ArrayLab(int integer)
{
    int[] array1 = new int[integer];
}

这就是所谓的构造函数。您传递整数,并作出阵列名为数组1 这是本地此范围,因此比以前的不同。这数组1 包含整数 -many零。

This is called a constructor. You are passing in integer, and making a new array called array1 which is local to this scope, therefore different than the one before. This array1 contains integer-many zeros.

要使用,并初始化previous 数组1 ,改变你的code到这里本

To use and initialize the previous array1, change your code up to here to this

int[] array1;
public ArrayLab(int integer)
{
    this.array1 = new int[integer];
}

接下来,

public void search(int integer)
    {
        int[] array1= new int[]{integer};
    }
}

这再次,创建一个新的数组,但只有一个值。所以说,整数为2,那么 [2]

This, again, creates a new array, but only one value. So say integer was 2, then [2].

这篇关于如何访问在阵列的方法的参数? Java的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆