views:

459

answers:

3

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

+1  A: 

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>();
Tom Dignan
i am pretty confused now, why are using string. and what does <String> means
@theband, I had updated my answer to hopefully clear up some confusion, if you have any other questions regarding it, I will be more than happy to help.
Anthony Forloney
+6  A: 

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)

Anthony Forloney
What does String mean here.... can i use int and other datatypes too
You can't use `int` but you would have to use `Integer`, when you use `< >` notation, it expects a *class* (`Integer`) not a primitive type (`int`)
Anthony Forloney
Thats a great riposte, thanks for your wee. Can you show me your example of monster class running with ArrayList or any other so that i can see the full code. Thanks
My code snippet shows how I used an `ArrayList` with my `Monster` class, I am not quite sure which other code you wanted to see.
Anthony Forloney
a running example of what you posted. it would be helpful for my study...
I can't post the whole project, theres 9-10 classes and one abstract class, but what you have now should be good enough as a running example, if not each data structure in question, I have linked to examples.
Anthony Forloney
Thanks, i was not asking for the entire project. you created a class monster, now calling the monster class in another class and then using the instance there. I just wanted to know how you would have done it, as it would have been made me more strong.Thanks a lot for your time,
@theband, I edited my answer to provide, what I hope to be, is a running version. There is `Monster` class and a `Main` class.
Anthony Forloney
Loch Ness monser is 20 years old? ha
Rubys
@Rubys, I wanted to make him younger, he's self conscience about that.
Anthony Forloney
A: 

hi. on your monster example , let's say that you have to create an n number of monsters and you have to insert the name, location ,age from the keyboard. how do you to that? thanks!

lox
@lox, This seems better fit for another question entirely, you shouldn't type a question in the answer box.
Anthony Forloney
Anthony, let me know that example too... i am learning a lot of things from you