[英]How to access parameters in a method for an array? Java
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);
}
}
The class ArrayLab has a method assigned to it called search with parameter of (2). ArrayLab类具有分配给它的方法,该方法称为搜索,其参数为(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));
}
}
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 :)
谢谢 :)
Your Driver
class is good. 您的
Driver
类很好。
So, lets take one line at a time 所以,让我们一次走一条线
int[] array1 = new int[10];
Okay, you made a public int array of size 10, more precisely [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
. 好的,您创建了一个公共int数组,大小为10,更确切地说
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
。
public ArrayLab(int integer)
{
int[] array1 = new int[integer];
}
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. 您要传入
integer
,并创建一个名为array1
的新数组,该数组在此作用域内是本地的,因此与以前的数组不同。 This array1
contains integer
-many zeros. 该
array1
包含integer
许多零。
To use and initialize the previous array1
, change your code up to here to this 要使用和初始化以前的
array1
,请将代码更改为此处
int[] array1;
public ArrayLab(int integer)
{
this.array1 = new int[integer];
}
Next, 下一个,
public void search(int integer)
{
int[] array1= new int[]{integer};
}
}
This, again, creates a new array, but only one value. 同样,这将创建一个新数组,但只有一个值。 So say
integer
was 2, then [2]
. 假设
integer
为2,则[2]
。
I don't know what the purpose of your ArrayLab class is , but here are some problems 我不知道ArrayLab类的目的是什么,但是这里有一些问题
In the constructor you are initializing a local array1
not your instance variable . 在构造函数中,您正在初始化本地
array1
而不是实例变量。
search
method is doing nothing but again initializing a local array1
. search
方法除了重新初始化本地array1
什么都不做。
Alright, so whats happening is in your class Driver your creating a object of your class ArrayLab. 好了,那么在您的类驱动程序中发生了什么,您在创建类ArrayLab的对象。 You send this class a constructor which creates a local variable array1.
您向此类发送构造函数,该构造函数创建局部变量array1。 Your search class initializing another local array1 this is what i would do for your ArrayLab class
您的搜索类将初始化另一个本地array1,这就是我为您的ArrayLab类所做的
import java.util.Arrays;
public class ArrayLab
{
int[] array1;
//array constructor
public ArrayLab(int integer)
{
this.array1 = new int[integer];
}
//method
public void search(int integer)
{
System.out.println(array1[integer]);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.