i'm trying to come up with an add method for a generic class i created. generally the object i create will be arrays, of type integer double or string.

i dont know how to go about making the add method to have stuff added to the array... heres my code so far...

/**
   The MyList class holds X and Y coordinates. The data type
   of the coordinates is generic.
*/

    public class MyList<T extends Number>
   {
      private T xCoordinate;  // The X coordinate
      private T yCoordinate;  // The Y coordinate
   
   /**
      Constructor
      @param x The X coordinate.
      @param y The Y coordinate.
   */
   
       public MyList(T x, T y)
      {
         xCoordinate = x;
         yCoordinate = y;
      }
   
   /**
      The setX method sets the X coordinate.
      @param x The value for the X coordinate.
   */
   
      
         //INSERT INFO HERE
         //PLEASE HELP!!!
          public void add(T[] array, int i)
      {
         int[] newArray = new int[array.length];
         System.arraycopy(array, 0, newArray, 0, array.length);
         newArray[newArray.length - 1] = i;
         return newArray;
      }

     
   
   }

I keep getting a compiler error... with this code above does it meet the requirements of my assignment:
ASSIGNMENT:
Write a generic class named MyList, with a type parameter T. The type parameter T should be constrained to an upper bound: the Number class. The class should have as a field an ArrayList of T. Write a public method named add, which accepts a parameter of type T. When an argument is passed to the method, it is added to the ArrayList.

Edited 6 Years Ago by xxunknown321: MORE CODE!!!

no, it does not meet the requirements.
Not only does it not do anything like that the requirements state, but no code that doesn't even compile should ever be good enough to meet requirements.

The class should have as a field an ArrayList of T. Write a public method named add, which accepts a parameter of type T. When an argument is passed to the method, it is added to the ArrayList.

Where in your code did you implement that requirement?

You need to make sure that you actually want an arrayList your code is not reflecting the title of your post. So you would 1st:

import java.util.*;

in one fashion or another...
Then:

ArrayList<Payday> myList = new ArrayList<Payday>();

make some sort of reference to an actual "arrayList". In my case I have created an arrayList called "myList" with the object Payday declared, it could easily be String or something else. Hope this is helpful, im a little unclear about your post

myList.add(object);

or

myList.add(index, object);

check java api
-jamesonh20

Edited 6 Years Ago by jamesonh20: n/a

i had this on a final today... You can use a setMyList method and just create a for loop recalling that method passing everything you need inside the setMyList(w.e variables); Please correct me if im wrong.. Im just guessing this is what you need by what you said.

This article has been dead for over six months. Start a new discussion instead.