0

It is necessary to use generics with ArrayList? Like this:

ArrayList<Object> software=new ArrayList<Object>();       

Can I write it without it? Because I face a problem when I want to add object to it.

What I try to do is to get info from the frame and then create object and add it to tha arraylist

class Listener implements ActionListener {
  public void actionPerformed(ActionEvent e) {
    ArrayList software= new ArrayList();

    String s = (String) major.getSelectedItem();

    if((e.getActionCommand()).equals("SAVE")){
      int st_id=Integer.parseInt(id.getText());
      String st_name=name.getText();
      String st_gender = (String) gender.getSelectedItem();
      String st_major = (String) major.getSelectedItem();
      String code1=code_sw1.getText();
      String code2=code_sw2.getText();
      String code3=code_sw3.getText();
      double mark1=Double.parseDouble(m_sw1.getText());
      double mark2=Double.parseDouble(m_sw2.getText());
      double mark3=Double.parseDouble(m_sw3.getText());
      St_Sw ob1=new St_Sw(st_id,st_name,st_gender,st_major,code1,code2,code3,mark1,mark2,mark3);
      software.add(ob1);
    }
  }
}
Tudor Timi
  • 7,453
  • 1
  • 24
  • 53

5 Answers5

4

What you mean is not parameter, it's a generic type. You can do a raw list like this:

List list = new ArrayList();

and it will work fine however the whole point of introducing generics in Java was to "fix" raw lists. If you store different objects than Object then it might go wrong when you for example iterate through the list and call one method on each element.

Lucas
  • 3,181
  • 4
  • 26
  • 45
2

Since Java 7 you can use the diamond operator:

 ArrayList<Object> software=new ArrayList<>(); 
aurelius
  • 3,946
  • 7
  • 40
  • 73
  • @LuiggiMendoza Minor edits are encouraged, we *should* as long as we don't change critical content. – Maroun Dec 22 '14 at 15:42
  • @MarounMaroun look at this as a non-Java developer. If I edited this wrongly and used 6 instead of 7, I would try it and then have an error. I think that indicating software version is not a minor edit, but probably this is highly debatable and I don't want to start a flame war here. – Luiggi Mendoza Dec 22 '14 at 15:43
2

It technically isn't necessary, and you can make an ArrayList without a type and have everything work just fine.

That being said, you should consider it necessary and whenever you run into a situation where you have to not type it, you are probably doing something wrong. Chances are you should look into polymorphism/inheritance (http://en.wikipedia.org/wiki/Polymorphism_%28computer_science%29), or interfaces (http://docs.oracle.com/javase/tutorial/java/concepts/interface.html) so that you can store multiple types of objects in the same ArrayList.

The main reason you want to type all of your ArrayList is so that Java will tell you when you try to so something wrong. At first, you might think that this is causing the problem, and making it stop yelling at you is always better than it not compiling. In reality, though, if Java is yelling at you, you are doing something wrong.

In your case, it appears that the arrayList only contains St_Sw Objects. If that is the case, you would want to make your ArrayList with:

ArrayList<St_Sw> software=new ArrayList<St_Sw>();
WiErD0
  • 467
  • 4
  • 16
0

Arraylist is a dynamic array with means that new memory is allocated when you add new items to the list. There is no need to define the size before creating a arraylist. To add a object just call the add method for software.

Icy Creature
  • 1,875
  • 2
  • 28
  • 53
0

As mentioned before,

List list = new ArrayList();

but also:

List<String> list = new ArrayList();

Is valid code in Java, and is supported for backwards compartibility reasons, as Java started having raw, non-generic types for structures like List etc. Non-generic types do not provide safety of type checking in compile time and should be avoided in new code, in general (Look here for more).

However, you can use diamond type inference for a shorthand, putting an "empty" diamond on the right side.

In Java, this

List<String> list = new ArrayList<>();

Is equal to this

List<String> a = new ArrayList<String>();  
Community
  • 1
  • 1
Nick Louloudakis
  • 5,856
  • 4
  • 41
  • 54