1

I'm trying to write a Linear List based on arrays, but make the list be able to store any value by using Java Generics. This way I can create other programs that utilize it, but pass in different data types. I'm not entirely sure how to do this, any help would be appreciated.

I guess Im struggling trying to set it up and create the functions. The generic type really messes me up. For example, trying to add a removeFirst() function, I cant use a loop like this:

for (int i = 0; i < n - 1; i++)
    newList[i] = newList[i + 1];

— as it says The type of the expression must be an array type but it resolved to ArrayList.

Fair warning, I'm still learning data structures. This is what I have so far:

import java.util.ArrayList;

public class LinearList<T> {
    private static int SIZE = 10;
    private int n = 0;
    private final ArrayList<T> newList = new ArrayList<T>(SIZE);
    private T t;

    public void set(T t) {
        this.t = t;
    }

    public T get() {
        return t;
    }

    public void add(T value, int position) {
        newList.add(position, value);
        n++;
    }

    public void addFirst(T value) {
        newList.add(0, value);
        n++;
    }

    public void removeLast() {
        T value = null;
        for (int i = 0; i < newList.size(); i++)
            value = newList.get(i);
        newList.remove(value);
        n--;
    }

    public void removeFirst() {
        newList.remove(0);
        n--;
    }

    public T first() {
        return newList.get(0);
    }

    public T last() {
        int value = 0;
        for (int i = 0; i < newList.size() - 1; i++)
            value++;
        return newList.get(value);
    }

    public int count() {
        return n;
    }

    public boolean isFull() {
        return (n >= SIZE);
    }

    public boolean isEmpty() {
        return (n <= 0);
    }

    //part 4
    public void Grow() {
        int grow = SIZE / 2;
        SIZE = SIZE + grow;
    }

    public void Shrink() {
        int grow = SIZE / 2;
        SIZE = SIZE - grow;
    }

    public String toString() {
        String outStr = "" + newList;
        return outStr;
    }
}
Liam
  • 29
  • 6
  • What do you mean by `linear list`? A method that takes an array and returns a list? – WJS May 20 '20 at 20:21
  • Which bit are you having trouble with? Declare you class generic. Implement using `Object[]` (array of `Object`). Keep a “top” pointer, a variable that you may interpret simultaneously as the number of elements in the list and as the index of the first vacant array position. Cast to your generic type when you take out elements. – Ole V.V. May 20 '20 at 20:25
  • I guess Im struggling trying to set it up and create the functions. The generic type really messes me up – Liam May 20 '20 at 20:54
  • For example, trying to add a removeFirst() function, I cant use a loop like this: `for (int i = 0; i < n - 1; i++) newList[i] = newList[i + 1];` as it says The type of the expression must be an array type but it resolved to ArrayList – Liam May 20 '20 at 20:55
  • Is `newList` declared an `ArrayList`? Sounds like you are implementing a linear list based on an existing linear list (in turn based on an array). If that’s a good exercise for you, do that. If you want to base your list directly on an array instead, declare `newList` an array, `Object[]`, as I said in my previous comment. – Ole V.V. May 21 '20 at 05:27
  • Frankly I think you can post a better question than this one. I suggest you post your best failed attempt, either the full code or a cut-down version that still exhibits the same error messages. And specify exactly what the error messages are and where in the code they appear. Then we’ll have something very concrete to help you with, which I expect to work better. See [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – Ole V.V. May 21 '20 at 05:30
  • @OleV.V. To be completely honest the last time I posted a question here with my best failed attempt, the only replies I got were telling me that I shouldn't be coding and what I failed at "I should know by now." So I tried to ask the question in a vague manner, in hopes I wouldn't receive the same hate. – Liam May 21 '20 at 20:19
  • I will update my question with what I've tried, but seriously. When I say I'm new to this I mean it. I really am unsure what to do, how to do it, and how it would work. – Liam May 21 '20 at 20:21
  • It’s good to come back after the weekend to see your question reopened after you followed my suggestion — or part of it. I am still missing which error message you are getting and where in the code they come. By reading through your code I don’t see which lines should give rise to any error message. Also my Eclipse doesn’t complain about your code, it believes that it is correct. – Ole V.V. May 24 '20 at 17:36

1 Answers1

0

A good start would be to make it non-generic with a class you are comfortable with, such as an Integer.

Once you have it set up, you can then make it generic by adding <T> to the class name, then replacing all references of Integer with T.

public class MyArray{ becomes public class MyArray<T>{

public Integer add(Integer value){ becomes public T add(T value){

See What are Generics in Java? for more help

Jack Cole
  • 1,528
  • 2
  • 19
  • 41