0

enter image description here

I construct an Hyper graph using jung library

  Hypergraph hypergraph = new SetHyperGraph<Vertex,HyperEdge>();

then I add all my vertex (image , tag , location)

    hypergraph.addVertex()

    ArrayList<Vertex> allVertex = hypergraph.getVertices;

Now I would to extract only image Vertex from my listallVertex

why this instruction is illegal?

    ArrayList<ImageVertex> allImageList=allVertex.subList(0,j);

Does Anyone have another solution?

nawara
  • 1,157
  • 3
  • 24
  • 49
  • Because `allVertex` may also contain `TagVertex` and `ImageVertex` and java is unable to figure it out at the compile time. – lifus Apr 25 '13 at 19:50
  • Use for loop with instanceof. Btw, tarrsalah answered the question much clearer than I did. – lifus Apr 25 '13 at 19:56
  • What does the word "concrect" mean in the title? – Gabe Apr 25 '13 at 19:57
  • As it is shown in the uml diagram abstract product=`Vertex ` and concret peoduct are `Image`,`tag`... – nawara Apr 25 '13 at 19:59

2 Answers2

1

Because ArrayList<ImageVertex> is not a subtype of ArrayList<Vertex>.

possible solution will be :

ArrayList<ImageVertex> allImageList= new ArrayList<>();

for (Vertex vertex: allVertex.sublist(0,j)) {
    if (vertex instanceof ImageVertex) {
        allImageList.add((ImageVertex) vertex);
    }
}
Salah Eddine Taouririt
  • 24,925
  • 20
  • 60
  • 96
  • try to loop throw all the `Vertex` list, and get the `ImageVertx` elements using the `instanceof` method. – Salah Eddine Taouririt Apr 25 '13 at 20:02
  • I believe that j is amount of ImageVertex, but it seems that nawara wants to extract all image vertexes. Also, please add cast to `ImageVertex`. – lifus Apr 25 '13 at 20:21
  • j is number of all my Image Vertex in fact mu idea is to sort `allVertex` alphabetically (image->location->tag) then as i know `j` value , i extract the first `j` vertex from `allVertex` list – nawara Apr 25 '13 at 20:26
  • Alright, so you might use this approach or simply use single for-each loop instead of sort, sublist and loop. – lifus Apr 25 '13 at 20:33
1

Just a sample code

List<ImageVertex> allImageList = new ArrayList<ImageVertex>();
for (Vertex vertex : allVertex) {
  if (vertex instanceof ImageVertex) {
    allImageList.add((ImageVertex) vertex);
  }
}
lifus
  • 8,074
  • 1
  • 19
  • 24