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

Ads
 

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());
        }
    }
}

Ads









Related Tutorials/Questions & Answers:
Is there a data structure like a class that doesn't need to be defined as a class in it's own file?
class with in its own objects?
Advertisements
user defined date class
user defined date class
USER DEFINED CLASS
compiling .class files
Class files for Jfreechart - JSP-Servlet
user defined subclass of throwable class - Java Beginners
How do I decompile Java class files?
Can a abstract class be defined without any abstract methods?
Class
class
Using a user defined non-packaged servlet class in jsp. - JSP-Servlet
Java writer class
class
Java Nested Class
Class in Java
Data structure
PHP Class Object
How to convert many class files into one exe file?
How to convert many class files into one exe file?
Why we need reload application after class change?
Use of Local Inner class
Sending large data to Action Class error. Struts code - Struts
Unable to get data from class - Development process
Class
inner class
Super class of an Exception class
need help for writting code in struts action class for check boxes and radio buttons - Struts
objective c extending a class with a class
objective c extending a class with a class

Ads