Is there a data structure like a class that doesn't need to be defined as a class in it's own file?

I am new to java, and I am trying to make a complex program with ArrayLists of objects. Within the classes of these objects, I would like to categorize my variables to better organize my program and to make ArrayLists easier. For example, I want to create the class Equipment which includes such factors as Attack, Durability, Charge, etc. I want to make a variable number of Charges, so normally I would make ArrayLists of each factor: chargename, charge, chargemax, etc. and simply add one to each factor every time I add another charge. However, I recall once seeing a data structure that one could create like a class without having to make a whole new file to define it, so I could simply make one ArrayList that included each variable I need. Does such a data structure exist, as I may be mistaken and cannot find any traces of this online, and how does it function exactly, as I recall it is defined upon creation with a block where variables are declared?

View Answers

May 3, 2012 at 12:25 PM

import java.util.*;
class Equipment 
{
    String attack;
    double durability;
    double charge;

      Equipment(String attack,double durability,double charge){
          this.attack=attack;
          this.durability=durability;
          this.charge=charge;
      }
      public String getAttack(){
          return attack;
      }

       public double getDurability(){
          return durability;
      }

       public double getCharge(){
          return charge;
      }

    public static void main(String[] args) 
    {
        ArrayList<Equipment> list=new ArrayList<Equipment>();
        list.add(new Equipment("A",2,1000));
        list.add(new Equipment("B",4,2000));

        for(Equipment e: list){
        System.out.println(e.getAttack()+"\t"+e.getDurability()+"\t"+e.getCharge());
        }
    }
}









Related Tutorials/Questions & Answers:
Advertisements