[英]Need some assistance with java stacks
Have to say i'm a total newbie to java. 不得不说我是Java的新手。 recently i'm studying implementing data structures using java for an exam.
最近,我正在研究使用Java实现考试的数据结构。 while going through array based stacks i found below code.
通过基于数组的堆栈时,我发现下面的代码。
class StackX {
private int maxSize; // size of stack array
private long[] stackArray;
private int top; // top of stack
//-------------------------------------------------------------
public StackX(int s) // constructor
{
maxSize = s; // set array size
stackArray = new long[maxSize]; // create array
top = -1; // no items yet
}
//-------------------------------------------------------------
public void push(long j) // put item on top of stack
{
stackArray[++top] = j; // increment top, insert item
}
i have an idea about what first three lines for, but i'm troubled at understanding below lines. 我对前三行的用途有所了解,但在理解以下几行时遇到了麻烦。
maxSize = s; // set array size
stackArray = new long[maxSize]; // create array
top = -1; // no items yet
Can someone explain why the use of [maxSize] in a array which is data type of long ? 有人可以解释为什么在数据类型为long的数组中使用[maxSize]吗? shouldn't it be numeric since the data type is long.
因为数据类型很长,所以它不应该是数字的。
also why use long j on push(long j) there's no variable associated with j. 也是为什么在push(long j)上使用long j没有与j相关联的变量。
Help & comments are appreciated. 帮助和评论表示赞赏。
I suggest searching out some websites or books and start from the beginning, trying to just jump in and understand everything doesn't tend to work for most people, but in answer to your questions: 我建议搜索一些网站或书籍,并从头开始,尝试跳入并了解所有内容对大多数人而言并不奏效,但要回答您的问题:
stackArray = new long[maxSize];
creates a new array of size 'maxsize' that can hold variables of the type long
. 创建一个大小为'maxsize'的新数组,该数组可以容纳
long
类型的变量。 'maxsize' is of type int, which is a whole number, and the array can only have a whole number of elements in it. 'maxsize'是int类型,它是一个整数,并且数组中只能包含整数个元素。
long j
is used because it means a method must be called at some point push(1.0)
which calls the push method and passes in 1.0
as j
. long j
是因为它意味着必须在某个时刻调用方法push(1.0)
,该方法将调用push方法并以j
传入1.0
。 It then puts it in the array at position top +1. 然后将其放置在数组中+1的位置。
Can someone explain why the use of [maxSize] in a array which is data type of long?
有人可以解释为什么在数据类型为long的数组中使用[maxSize]吗? shouldn't it be numeric since the data type is long.
因为数据类型很长,所以它不应该是数字的。
maxSize
is an integer variable, used to specify the size of the array which is created. maxSize
是一个整数变量,用于指定创建的数组的大小。 So this statement: 所以这条语句:
stackArray = new long[maxSize];
creates an element with maxSize
elements (eg 20) and then assigns a reference to that array to the stackArray
variable. 创建一个具有
maxSize
元素(例如20)的元素,然后将该数组的引用分配给stackArray
变量。
It's not really clear what you were expecting otherwise, but I'd suggest reading the Java tutorial section on arrays . 目前还不清楚您期望的是什么,但是我建议阅读有关数组的Java教程部分 。
also why use long j on push(long j) there's no variable associated with j.
也是为什么在push(long j)上使用long j没有与j相关联的变量。
This method declaration: 此方法声明:
public void push(long j)
has a parameter called j
. 有一个名为
j
的参数 。 That means whenever you call the method you have to pass in a long
value as the argument to the method. 这意味着无论何时调用该方法,都必须传递一个
long
值作为该方法的参数 。 The initial value of the parameter for that method invocation is the same value that you pass in. From that point onwards, it's like a local variable for that method. 该方法调用的参数初始值与您传入的值相同。从那时起,它就像该方法的局部变量一样。 Again, the Java tutorial on methods may help you.
同样, 有关方法的Java教程可能会对您有所帮助。
Can someone explain why the use of [maxSize] in a array which is data type of long? 有人可以解释为什么在数据类型为long的数组中使用[maxSize]吗? shouldn't it be numeric since the data type is long.
因为数据类型很长,所以它不应该是数字的。
Although your array might be declared as type long
, the indexes are still int
. 尽管您的数组可能被声明为
long
类型,但是索引仍然是int
。 No matter what is the type of your array, indexes are int
. 无论您的数组是什么类型,索引都是
int
。 Look at the examples on this page for more information. 请参阅此页面上的示例以获取更多信息。
also why use long j on push(long j) there's no variable associated with j. 也是为什么在push(long j)上使用long j没有与j相关联的变量。
Look for the code that calls this method push()
, you will find a variable associated with j
. 查找调用此方法
push()
的代码,您将找到与j
相关联的变量。 And since your array is of type long
,when you put in elements into this(push), they have to be of long
type too. 而且由于数组是
long
类型的,因此当您将元素放入this(push)中时,它们也必须是long
类型的。 Hence, long j
. 因此,
long j
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.