user3287300
user3287300

Reputation: 151

Abstract Data Type: Array of objects

So I have this class, and I want to method that takes in an int, and creates a new array of that size. If I declare

newArray<Object<int,int>> array1 = new newArray<Object<int,int>>(10);

This would create an array of size 10.


I've tried doing

    public class newArray<O>
    {

        private O[] array;

        public newArray(int newArraySize) 
          {
             newArray<O>[] = new newArray<O>[newArraySize];
          }
}

but I get an " Cannot create a generic array of newArray " error.

Upvotes: 0

Views: 2455

Answers (3)

awksp
awksp

Reputation: 11877

Unfortunately, you can't create a generic array. This is due to the way generics are implemented in Java, which is through type erasure. In effect, all generic types are "erased" to their bounding type (usually Object) before compilation, so all the compiler sees are Object instead of T or E or O. The erasure process generates automatic casts to ensure that the program would still work as intended.

This means that you can't do:

  • new O() (this gets erased to new Object(), which the compiler has no idea what to do with)
  • new O[] (this gets erased to new Object(), which again isn't helpful to the compiler)

What you can do is casting:

array = (O[]) new Object[size];

And in fact, that's how it's done in Java's Collections framework. You'll get an "unchecked" warning, as the compiler can't prove that this conversion is safe, but there really is no other option.

Also, few things I want to point out about your question, that you may or may not know already:

  • You can't use primitives as type parameters
  • Object has no type parameters
  • newArray<O>[] = new newArray<O>[newArraySize]; should really be array = new newArray<O>[newArraySize];. You already declared the array you wanted to use. So use it!

Looks like you're implementing your own ArrayList, in fact. If you are, good luck! If you aren't, you should really be using the existing implementation, unless you need some special behavior that you can't get otherwise...

Upvotes: 1

Mohammad Najar
Mohammad Najar

Reputation: 2009

Does this help?

    ArrayList<Integer[][]> r = new ArrayList<Integer[][]>(10);
    // You could replace the above list with a custom list

    Integer[][] ob = new Integer[1][2];
    ob[0][0] = 10;
    ob[0][1] = 20;

    r.add(ob);


    for(Integer[][] o : r)
        for(Integer[] o1 : o)
            for(Integer o2 : o1)
                System.out.println(o2);

And the output is:

10
20

Upvotes: 0

Alexandre Santos
Alexandre Santos

Reputation: 8338

You are almost there.

Take a look at the following code for an example on how to initialize, populate, and print an array.

import java.util.*;

public class Test 
{
    public static void main(String[] args)
    {
        // define the array
        List<Integer> array1 = new ArrayList<Integer>(10);

        // initialize the array with some value. In this case, the Integer "200"
        // note that the array doesn't have a hard limit of 10 as define above. You can change the following value to 20 and it will still work
        for (int i = 0; i < 10; i++)
        {
            array1.add(i, 200);
        }

        // print the array
        System.out.println(array1);
    }
}

Upvotes: 0

Related Questions