Forum Stats

  • 3,722,791 Users
  • 2,244,415 Discussions
  • 7,850,094 Comments

Discussions

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Array or ArrayList ?

25ff03f5-c9c5-4664-afa2-81d36adb4ba8
edited October 2018 in New To Java

Hello Everybody,

I am quite new to Java, and I am working on a little project for myself to extend my knowledge skills in Java.

I am developing a little Programm called "Team Fees" for our team, where I can store the fees each player has.

The fees differentiate in 3:

  1. General fee
  2. Training fee
  3. GameDay fee

For Each of those I made a class.

  1. GeneralFine
  2. TrainingFine
  3. GameDayFine

For Each of those classes, they have Attributes FeeId, FeeDescription and FeeAmount.

Let's stick at first to the GeneralFine class.

I have a list with 12 fines for the GeneralFine class.
Each fine should have a FeeId, FeeDescription and an FeeAmount.

My Idea is that when I instantiate an Object from the Class GeneralFine, then the Object also should have an FeeId, FeeDescription and FeeAmount.

In the class I want to declare the FeeId, FeeDescription and FeeAmount. I am not sure tho how to do it the best and efficient way, shall I take Array or ArrayList ?

FeeId is of String Type

FeeDescription is also of String Type

FeeAmount is of double Type.

I want to make a list for the FeeDescription and FeeId and when I have it, I want to assign each object of the GeneralFine class a FeeId and FeeDescription from that Array.

Because at the End, one or more Player can have the same fees.

Tagged:
TPD-Opitzjwenting

Answers

  • Unknown
    edited October 2018

    The first thing to do is to write code that is modular and that actually works.

    You also need to learn about arrays and ArrayLists, the differences between them and the typical use cases that each are used for.

    The Java Tutorials has trails on how to use ALL of the basic functionality of Java.

    A simple web search for 'the java tutorials array' or 'the java tutorials arraylist' will return the link you need.

    https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

    https://docs.oracle.com/javase/tutorial/collections/implementations/list.html

    I am quite new to Java

    The BEST, and often fastest, way to get an answer to a question like that is DO IT AND SEE!

    If you are new to Java then do it both ways and compare them yourself. You need to learn about both native arrays and collections anyway so there is no reason to put it off.

    Pick one of them and implement it. Then convert it to use the other type. That way you will learn what is involved.

    Good luck with your project.

    TPD-Opitz
  • Tharaka Uchiha
    Tharaka Uchiha Member Posts: 21
    edited October 2018

    My opinion is, you should first go to the youtube and check sum tutorials about arrays and arrayList and check below sample code

    public class GeneralFine {

    private int genFeeId;

    private String genFeeDescription;

    private double genFeeAmount;

    public int getGenFeeId() {

    return genFeeId;

    }

    public void setGenFeeId(int genFeeId) {

    this.genFeeId = genFeeId;

    }

    public String getGenFeeDescription() {

    return genFeeDescription;

    }

    public void setGenFeeDescription(String genFeeDescription) {

    this.genFeeDescription = genFeeDescription;

    }

    public double getGenFeeAmount() {

    return genFeeAmount;

    }

    public void setGenFeeAmount(double genFeeAmount) {

    this.genFeeAmount = genFeeAmount;

    }

    @Override

    public String toString() {

    return "GeneralFine [genFeeId=" + genFeeId + ", genFeeDescription=" + genFeeDescription + ", genFeeAmount="

    + genFeeAmount + "]";

    }

    }

    ---------------------------------------------------------------------------------------------------------------------------------------------------------------

    public static void main(String[] args) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {

    List<GeneralFine> genList = new ArrayList<>();

    //Adding 10 objects of GenealFine Class to ArrayList genList

    for (int i = 1; i <= 10; i++) {

    GeneralFine objGF = new GeneralFine();

    objGF.setGenFeeId(i);

    objGF.setGenFeeDescription("Description "+i);

    objGF.setGenFeeAmount(200);

    genList.add(objGF);

    }

    Iterator<GeneralFine> objIterator = genList.iterator();

    while(objIterator.hasNext()) {

    System.out.println(objIterator.next().toString());

    }

    }

  • TPD-Opitz
    TPD-Opitz Member Posts: 2,465 Silver Trophy
    edited October 2018
    Tharaka Uchiha wrote:My opinion is, you should first go to the youtube and check sum tutorials about arrays and arrayList 

    The first and most reputational source for Java related tutorials is the official Java Tutorial website as mentioned by @rp0428 in his answer, not any random video on the internet.

    Tharaka Uchiha wrote:and check below sample code

    Whenever you post code, please make sure you use the code syntax highlighting available via the advanced editor.

    bye

    TPD

    jwenting
  • nihitthakkar
    nihitthakkar Member Posts: 96 Red Ribbon

    An array is basic functionality provided by Java with fixed length data structure. Whereas ArrayList is part of collection framework in Java with variable length.


    Following are two different ways to create an array and arraylist:


    1. Array: Simple fixed sized arrays that we create in Java, like below

       int arr[] = new int[10]  


    1. ArrayList : Dynamic sized arrays in Java that implement List interface.

       ArrayList<Type> arrL = new ArrayList<Type>();

       

Sign In or Register to comment.