如何在Java中创建一个Array,ArrayList,Stack和Queue? [英] How to create an Array, ArrayList, Stack and Queue in Java?

查看:424
本文介绍了如何在Java中创建一个Array,ArrayList,Stack和Queue?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读Java文章,但在声明中没有发现任何差异,并且被混淆了。任何人都可以列出我吗?



添加文章



http://www.theparticle.com/javadata2.html

解决方案

没有关于什么问题正在提出的更多细节,我将回答问题的标题,



创建一个 Array

  String [] myArray = new String [2]; 
int [] intArray = new int [2];

//或可以声明如下
String [] myArray = {this,is,my,array};
int [] intArray = {1,2,3,4};

创建一个 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);

这意味着,创建一个 ArrayList code> String 和整数对象。你 不能 使用 int ,因为这是一个原始数据类型,请参阅原始数据类型列表的链接。



创建 堆栈

 堆栈myStack = new Stack(); 
//添加任何类型的元素(String,int等)
myStack.push(Hello);
myStack.push(1);

创建一个 队列 :(使用LinkedList )

 队列< String> myQueue = new LinkedList< String>(); 
队列<整数> myNumbers = new LinkedList< Integer>();
myQueue.add(Hello);
myQueue.add(World);
myNumbers.add(1);
myNumbers.add(2);

ArrayList 相同的东西,这个声明意味着创建一个 cue String Integer / p>




更新:



为了回应您对其他人的评论给出答案,


我现在很困惑,为什么要使用字符串。而< String> 意味着


我们使用 String 只是一个纯粹的例子,但你可以添加任何其他对象,但主要的是你使用一个对象 strong> 不是 一个原始类型。每个基本数据类型都有自己的原始包装类,请参阅原始数据类型的包装类列表的链接



我已经发布了一些链接来解释两者之间的区别,但是这里是一个原始类型的列表




  • byte

  • short li>
  • char

  • int li>
  • long

  • boolean li>
  • double

  • float li>


这意味着你不能像这样做一个整数的 ArrayList

  ArrayList< int> number = new ArrayList< int>(); 
^应该是一个对象,int不是一个对象,而是整数就是!
ArrayList< Integer> numbers = new ArrayList< Integer>();
^完全有效

此外,您可以使用自己的对象,这里是我的我创建的怪物对象

  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 +来自+ location +
,为+ age +旧。);
}
}

这里我们有一个怪物对象,但现在在我们的 Main.java 类中,我们要保留我们所有的 Monster 我们创建,所以我们将它们添加到一个 ArrayList

  public class Main {
ArrayList< Monster> myMonsters = new ArrayList< Monster>();

public Main(){
Monster yetti = new Monster(Yetti,The Mountains,77);
怪物lochness =新怪物(Lochness怪物,苏格兰,20);

myMonsters.add(yetti); //< - 将Yetti添加到我们的列表
myMonsters.add(lochness); //< - 添加Lochness到我们的列表

for(Monster m:myMonsters){
m.printDetails();
}
}

public static void main(String [] args){
new Main();
}
}

我帮助了我女朋友的兄弟Java游戏,他也必须做这些事情,但我希望这个例子被很好地证明了


I was reading a Java article, but found no differences in the declaration and was confused over. Can anyone list me out this?

Added the Article

http://www.theparticle.com/javadata2.html

解决方案

Without more details as to what the question is exactly asking, I am going to answer the title of the question,

Create an 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<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 cannot use int because thats a primitive data types, see the link for a list of primitive data types.

Create a 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<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.


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

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 object not a primitive type. Each primitive data type has their own primitive wrapper class, see link for list of primitive data type's wrapper class.

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

Which means, you are not allowed to make an ArrayList of integer's like so:

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,

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

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中创建一个Array,ArrayList,Stack和Queue?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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