如何在 Java 中创建数组、ArrayList、堆栈和队列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2709128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create an Array, ArrayList, Stack and Queue in Java?
提问by Kevin
I was reading a Java article, but found no differences in the declaration and was confused over. Can anyone list me out this?
我正在阅读一篇Java文章,但发现声明中没有任何差异,并且感到困惑。谁能给我列出来?
Added the Article
添加了文章
采纳答案by Anthony Forloney
Without more details as to what the question is exactly asking, I am going to answer the title of the question,
没有关于问题究竟问什么的更多细节,我将回答问题的标题,
Create an Array
:
创建一个Array
:
String[] myArray = new String[2];
int[] intArray = new int[2];
// or can be declared as follows
String[] myArray = {"this", "is", "my", "array"};
int[] intArray = {1,2,3,4};
Create an ArrayList
:
创建一个ArrayList
:
ArrayList<String> myList = new ArrayList<String>();
myList.add("Hello");
myList.add("World");
ArrayList<Integer> myNum = new ArrayList<Integer>();
myNum.add(1);
myNum.add(2);
This means, create an ArrayList
of String
and Integer
objects. You cannotuse int
because thats a primitive data types, see the link for a list of primitive data types.
这意味着,创建ArrayList
的String
和Integer
对象。您不能使用,int
因为那是原始数据类型,请参阅原始数据类型列表的链接。
Create a Stack
:
创建一个Stack
:
Stack myStack = new Stack();
// add any type of elements (String, int, etc..)
myStack.push("Hello");
myStack.push(1);
Create an Queue
: (using LinkedList)
创建一个Queue
:(使用 LinkedList)
Queue<String> myQueue = new LinkedList<String>();
Queue<Integer> myNumbers = new LinkedList<Integer>();
myQueue.add("Hello");
myQueue.add("World");
myNumbers.add(1);
myNumbers.add(2);
Same thing as an ArrayList
, this declaration means create an Queue
of String
and Integer
objects.
与 an 相同ArrayList
,此声明意味着创建一个Queue
ofString
和Integer
对象。
Update:
更新:
In response to your comment from the other given answer,
为了回应您在另一个给出的答案中的评论,
i am pretty confused now, why are using string. and what does
<String>
means
我现在很困惑,为什么要使用字符串。什么
<String>
意思
We are using String
only as a pure example, but you can add any other object, but the main point is that you use an objectnota primitive type. Each primitive data type has their own primitive wrapper class, see link for list of primitive data type's wrapper class.
我们String
仅用作纯示例,但您可以添加任何其他对象,但重点是您使用的是对象而不是原始类型。每个原始数据类型都有自己的原始包装类,请参阅原始数据类型包装类列表的链接。
I have posted some links to explain the difference between the two, but here are a list of primitive types
我已经发布了一些链接来解释两者之间的区别,但这里有一个原始类型列表
byte
short
char
int
long
boolean
double
float
byte
short
char
int
long
boolean
double
float
Which means, you are not allowed to make an ArrayList
of integer's like so:
这意味着,您不能ArrayList
像这样制作整数:
ArrayList<int> numbers = new ArrayList<int>();
^ should be an object, int is not an object, but Integer is!
ArrayList<Integer> numbers = new ArrayList<Integer>();
^ perfectly valid
Also, you can use your own objects, here is my Monster
object I created,
此外,您可以使用自己的对象,这是我Monster
创建的对象,
public class Monster {
String name = null;
String location = null;
int age = 0;
public Monster(String name, String loc, int age) {
this.name = name;
this.loc = location;
this.age = age;
}
public void printDetails() {
System.out.println(name + " is from " + location +
" and is " + age + " old.");
}
}
Here we have a Monster
object, but now in our Main.java
class we want to keep a record of all our Monster
's that we create, so let's add them to an ArrayList
这里我们有一个Monster
对象,但是现在在我们的Main.java
类中我们想要记录Monster
我们创建的所有's,所以让我们将它们添加到ArrayList
public class Main {
ArrayList<Monster> myMonsters = new ArrayList<Monster>();
public Main() {
Monster yetti = new Monster("Yetti", "The Mountains", 77);
Monster lochness = new Monster("Lochness Monster", "Scotland", 20);
myMonsters.add(yetti); // <-- added Yetti to our list
myMonsters.add(lochness); // <--added Lochness to our list
for (Monster m : myMonsters) {
m.printDetails();
}
}
public static void main(String[] args) {
new Main();
}
}
(I helped my girlfriend's brother with a Java game, and he had to do something along those lines as well, but I hope the example was well demonstrated)
(我帮助我女朋友的弟弟玩了一个 Java 游戏,他也必须按照这些思路做一些事情,但我希望这个例子得到了很好的证明)
回答by Kevin
I am guessing you're confused with the parameterization of the types:
我猜你对类型的参数化感到困惑:
// This works, because there is one class/type definition in the parameterized <> field
ArrayList<String> myArrayList = new ArrayList<String>();
// This doesn't work, as you cannot use primitive types here
ArrayList<char> myArrayList = new ArrayList<char>();
回答by Prashanth Peddabbu
Just a small correction to the first answer in this thread.
只是对该线程中第一个答案的一个小更正。
Even for Stack, you need to create new object with generics if you are using Stack from java util packages.
即使对于 Stack,如果您使用 java util 包中的 Stack,也需要使用泛型创建新对象。
Right usage:
Stack<Integer> s = new Stack<Integer>();
Stack<String> s1 = new Stack<String>();
s.push(7);
s.push(50);
s1.push("string");
s1.push("stack");
if used otherwise, as mentioned in above post, which is:
如果以其他方式使用,如上面的帖子所述,即:
/*
Stack myStack = new Stack();
// add any type of elements (String, int, etc..)
myStack.push("Hello");
myStack.push(1);
*/
Although this code works fine, has unsafe or unchecked operations which results in error.
尽管此代码运行良好,但存在不安全或未经检查的操作,从而导致错误。