George George - 1 year ago 80
Java Question

Java: Creating an array of Integer arrays using generic class

The following represents the code of a generic Queue class. Now, what I tried to do in the main, I tried to create an array of Integer arrays, using the Queue class. However, I have failed miserably in my attempt, due to the fact that I have not managed to properly add the elements in the array of Integer arrays to the queue.
Here's the code with some explanations:

The main:

import java.util.Iterator;

public class Main
{public static void main(String[] args)
{Queue <Integer[][]> Q= new Queue<Integer[][]>(); // creating the queue
Integer []i1={1,2,3};
Integer []i2={1,2,3};
Integer [][]i=new Integer[][]{i1,i2}; // creating the array of integer arrays

for(Iterator<Integer[][]> it=Q.iterator(); it.hasNext();){ System.out.print(" "); }

Iterator<Integer[][]> it=Q.iterator();;


And here is the Queue class:

import java.util.*;

public class Queue <T>
{LinkedList<T>queue=new LinkedList<T>();
public void add(T x){ queue.add(x); NOF++; } // the regular add function

public void add(T[][] x) // the add function in case we get an array of Integer arrays
{ int i=0,j=0,k=0; T [] v;
while(x[i][j]!=null) // checking if the are still availabe arrays
{ v=x[i][j]; //1 passing to v an array (or at least what I thought I'd pass)
//but i don't think it works
while(v[k]!=null) // while the arrays has elements, add them to the queue
{ queue.add(v);
k++; }
j++; } }

public void remove(T x){ queue.remove(); }
public T peek(T x){ return queue.peek(); }
public String toString(){ String S=""; for(T x: queue)S=S + x + " "; return S; }
static int NOF=0;

public class QueueIterator<T> implements Iterator<T>
{ int index=0;
public boolean hasNext(){ return index < queue.size(); }
public T next(){ return (T) queue.get(index++); }
public void remove(){ if(index>0)queue.remove(index-1); } }

public Iterator<T> iterator(){ return new QueueIterator<T>(); }


So it obviously doesn't work, but I don't understand why, at //1 I get the error:

Type mismatch, cannot convert from T to T[]

but I don't understand why, x[i][j] was supposed to be T[] as well.

Any ideas, homies?

Answer Source

Your problem is pretty simple (kinda surprising giving the overly complex coding style!)

It seems that you want to create a Queue that implicitly works with two-dim arrays of T:

class Queue<T> ...
  public void add(T[][] x)

But then you are instantiating that thing ... again with two-dim array:

Queue <Integer[][]> Q = ...

In that sense, you are "doubling" things up here.

You could simply change the queue usage to

Queue <Integer> Q = ...

to make things work!

But that would be the wrong solution. You see: you don't need to those array information at all.

Just make your Queue implementation deal with T objects. Using T[][] all over the place doesn't give you any value! Nothing in the Queue class code relies on the fact that you expect a two-dim array of T!

Meaning: you would put that information into your client (so, there you would keep saying Integer[][]. But then: that would also be wrong: there is no point in mixing arrays and collections. Just use List<Integer> or maybe List<List<Integer>> if you really need two dimensions.


A) first suggestion ... to fix your problem, turn into your Q class and simply remove any [][] you have in there. Then you can keep your Main class as is, and things should compile.

B) but then, you should consider changing your Main class, too - to make it use Lists instead of arrays!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download