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?
user defined date class
Advertisements
user defined date class
USER DEFINED CLASS
compiling .class files
Class
How do I decompile Java class files?
Java Nested Class
Can a abstract class be defined without any abstract methods?
Java writer class
class
Why we need reload application after class change?
ModuleNotFoundError: No module named 'data-class-detection'
Use of Local Inner class
How to convert many class files into one exe file?
How to convert many class files into one exe file?
inner class
objective c extending a class with a class
objective c extending a class with a class
abstract class
Class Loader
Class Loader
Explain final class, abstract class and super class.
Inner class
Data structure
Wrapper Class
generating mock data class of times (start and end time and value)
class distance
class name
Abstract Class in Java
file class

Ads