1

I've been creating an Arraylist as a public class, as that's how I've been taught. The functionality of the Arraylist when I refer to it in another document with out variables e.g.: get(), remove(), size() etc. Means I have to create it within the public class each time. Do I have to add code each time for each one, as at the moment I'm trying to create size(), but need some help.

Or is there a way to make the Arraylist function normally, not needing to add lines each time. The code should help explain what I mean:

import java.util.ArrayList;

public class Question1 {

    private ArrayList<Question1Entry> entries;

    public Question1() {
        entries = new ArrayList<Question1Entry>();
    }

    public void add( String name, String studentNumber, String courseName, String courseID, String houseNumber, String streetName, String town, String postcode ) {
        entries.add(new Question1Entry(name, studentNumber, courseName, courseID, houseNumber, streetName, town, postcode ));
    }

    public void remove (int index ) {
        entries.remove(index);
    }

    public Question1Entry get(int index) {
        return entries.get(index);
    }

    //The variable I need help creating 
    public Question1Entry size(int index) {
        return entries.size(index);
    }

    public String toString( ) {
        StringBuffer temp = new StringBuffer();
        for (int i = 0; i < entries.size(); ++i) {
            temp.append( entries.get(i).toString() + "\n" );
        }
        return temp.toString();
    }    
}

Is there a way to get around adding get(), size() remove() and so on?

Thank you :)

Neuron
  • 5,141
  • 5
  • 38
  • 59
JJ Wilson
  • 17
  • 4
  • 1
    Generics? That's what they're for. – Dave Newton Apr 12 '18 at 12:59
  • I am not quite sure I understand your question correctly. If I do, you want to have all basic `List`-methods available within `Question1`? If so, this might do the trick: `public class Question1 extends ArrayList` (and gets rid of the attribute `entries`). – Turing85 Apr 12 '18 at 13:00
  • 1
    "The variable I need help creating" What are you trying to write here? There is no `entries.size(int)` method. If `Question1Entry` has a `size` method, why not just call `get(index).size()`? – Andy Turner Apr 12 '18 at 13:00
  • One thing you could do would be to extend ArrayList `public class Question1 extends Arraylist` but I don't think that's a very clean solution... – Patric Apr 12 '18 at 13:01
  • 1
    @Tagas that's almost never the right thing to do. – Andy Turner Apr 12 '18 at 13:01
  • @AndyTurner could you elaborate why? – Turing85 Apr 12 '18 at 13:02
  • @Turing85 https://stackoverflow.com/questions/49002/prefer-composition-over-inheritance – Andy Turner Apr 12 '18 at 13:02
  • Myself, I do what you're doing, have my class contain an array list field, and expose only the ArrayList behaviors (decoration methods) that I want exposed. – Hovercraft Full Of Eels Apr 12 '18 at 13:02
  • @AndyTurner accidentally hit enter before i finished writing my comment. You're right, not a good idea. – Patric Apr 12 '18 at 13:03
  • @AndyTurner Hi, I'm basically very new to Java and this class is getting referred to by another document. But I need to refer to the size of 'Question1' (the arraylist), that's why I think i need to create that 'public... ' but not sure how to do so. – JJ Wilson Apr 12 '18 at 13:05
  • @Turing85 if i put the 'extend' part, will it also allow me to also override for example keeping my .add or will I refer to it as something else? – JJ Wilson Apr 12 '18 at 13:06
  • A solution based on composition would be some class, containing a `List<...>` as attribute and exposing the necessary methods. You could then reuse this class (basically what you did and @HovercraftFullOfEels mentioned, but in a separate class). – Turing85 Apr 12 '18 at 13:06
  • @JJWilson before using concepts, make sure you understand how they work. For inheritance, [the official Oracle tutorial](https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html) as well as [the one on interfaces](https://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html) are good places to start. – Turing85 Apr 12 '18 at 13:07

2 Answers2

0

What you have used so far is called delegation. You could continue to recreate each method and delegate it to your delegation object (entries). Many IDEs have functions to quickly create the delegation methods you need.

  • Intellij: SourceGenerateDelegate Methods

  • Eclipse: CodeGenerate Delegate Methods

I would also recommend using inheritance like in the example by Mr. Polywhirl (although you can do it without generics if that concept is new to you). The most obvious benefit will be that you'll have to define the methods only once, but the real benefit comes from generalizing your code to work on any question (not just Question1).

Neuron
  • 5,141
  • 5
  • 38
  • 59
  • Hi, thank you for enlightening me to what it's called. Can you recommend an IDE which might be helpful for this situation? – JJ Wilson Apr 12 '18 at 13:10
  • I personally use and love Intellij (they have free (as in money free) community edition). But Eclipse also has such a functionality – Neuron Apr 12 '18 at 13:12
0

If you use generics you can reuse your generic question class for more questions.

You can structure is as follows:

Question1Entry

public class Question1Entry implements GenericEntry {
    public Question1Entry(String name, String studentNumber, String courseName, String courseID, String houseNumber,
        String streetName, String town, String postcode) {
    }
}

Question1

public class Question1 extends GenericQuestion<Question1Entry> {
    public void add(String name, String studentNumber, String courseName, String courseID, String houseNumber, String streetName, String town, String postcode) {
        add(new Question1Entry(name, studentNumber, courseName, courseID, houseNumber, streetName, town, postcode));
    }

    @Override
    public void add(Question1Entry entry) {
        getEntries().add(entry);
    }
}

GenericEntry

public interface GenericEntry {

}

GenericQuestion

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public abstract class GenericQuestion <T extends GenericEntry> {
    private List<T> entries;

    public List<T> getEntries() { return entries; }
    public void setEntries(List<T> entries) { this.entries = entries; }

    public GenericQuestion() {
        entries = new ArrayList<T>();
    }

    // Could define it here, but we'll leave it for implementation.
    public abstract void add(Question1Entry entry);

    public void remove(int index) {
        entries.remove(index);
    }

    public T get(int index) {
        return entries.get(index);
    }

    public int size() {
        return entries.size();
    }

    public String toString() {
        return entries.stream().map(T::toString).collect(Collectors.joining(System.lineSeparator()));
    }
}
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132