3

I'm writing my custom Map, it have custom Pair array and Map uses the pair for operations.

They are generic, I don't know their's type it can be integer, string or double. So I can not use ArrayList, It's forbidden for me.

public class FMap<K, V> {
   private FPair<K, V>[] data;
   int capacity=23;
   int used=0;

   public FMap(int cap){
      super();
      capacity=cap;
      used =0;
      data = new FPair[ capacity];
      for(int i=0; i< data.length; ++i)
          data[i] = new FPair<K, V>();
}

But compiler saying:

javac -g -Xlint BigramDyn.java
./TemplateLib/FMap.java:23: warning: [rawtypes] found raw type: FPair
        data = new FPair[capacity];
                   ^
  missing type arguments for generic class FPair<A,B>
  where A,B are type-variables:
    A extends Object declared in class FPair
    B extends Object declared in class FPair
./TemplateLib/FMap.java:23: warning: [unchecked] unchecked conversion
        data = new FPair[capacity];
               ^
  required: FPair<K,V>[]
  found:    FPair[]
  where K,V are type-variables:
    K extends Object declared in class FMap
    V extends Object declared in class FMap
2 warnings

if I use data = new FPair<K, V>[capacity] instead of data = new FPair[capacity]

Compiler is saying :

TemplateLib/FMap.java:23: error: generic array creation
        data = new FPair<K,V>[capacity];
               ^
1 error

--

And in equal function of map: I'm doing: FMap

FMap<K,V> otherPair = (FMap<K,V>) other;

But compiler saying:

./TemplateLib/FMap.java:34: warning: [unchecked] unchecked cast
            FMap<A,B> otherPair = (FMap<A,B>) other;
                                                ^
  required: FMap<A,B>
  found:    Object
  where A,B are type-variables:
    A extends Object declared in class FMap
    B extends Object declared in class FMap
1 warning
1

1 Answer 1

0

Use an ArrayList as such:

List<FPair<K, V>> data = new ArrayList<>(capacity);

As ArrayList wraps an array you have all the comfort of List and the functionality of an array.

Only fill in an item when there is data, no new FPair<K, V>();.


As ArrayList is not allowed:

    data = (FPair<K, V>[]) new FPair<?, ?>[10];

The <?, ?> or <Object, Object> will satisfy the compiler (no longer a raw type FPair used). The abuse/cast is unlucky. But as types are stripped, there is no actual difference.

If you want to fill every item (not advisable):

    Arrays.fill(data, new FPair<K, V>());

    Arrays.setAll(data, ix -> new FPair<K,V>());

The first fills the same element at every positon, usefull when FPair is not changed but can be shared: when it is "immutable".

The second is just a fancy way to loop.

Sign up to request clarification or add additional context in comments.

2 Comments

very thanks but I can not use this because it is forbidden for me. My code is running like I want but it gives only some warnings, That's means, my code is not right, can not I use generic array like it?
@FurkanYıldız the warning doesn't necessarily mean your code is wrong, @SuppressWarnings is perfectly acceptable in some cases (mostly with generics)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.