[JAVA] clascastexception, ik zie em niet

Pagina: 1
Acties:

  • GiLuX
  • Registratie: Juni 1999
  • Laatst online: 12-11-2025
kijk:

ik heb een vector gevuld:
code:
1
2
3
4
5
private Vector childIds;
 
public void setChildIds(Long childId){
      childIds.addElement(childId);
}

ik weet nu heel zeker dat er dit in zit: [0,1,2]

vervolgens heb ik dit:
(object ef)
code:
1
2
3
     public Long[] getChildIds() {
      return (Long[]) childIds.toArray();
    }


en dan doe ik:
code:
1
Long[] cids = ef.getChildIds();

maar dit geeft een classcast exception maar waarom?????:
java.lang.ClassCastException: [Ljava.lang.Object;

ik zie het niet meer, zal ongetwijfeld iets doms zijn ;)
hlp plz!

"I disagree with what you are saying, but I will defend to the death your right to say it." -- not clear who


  • mbravenboer
  • Registratie: Januari 2000
  • Laatst online: 06-11-2025
Zie de API docs van Collection. Hier staat het goed uitgelegd. Je moet de methode met de parameter hebben:
Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array. If the collection fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this collection.

If this collection fits in the specified array with room to spare (i.e., the array has more elements than this collection), the element in the array immediately following the end of the collection is set to null. This is useful in determining the length of this collection only if the caller knows that this collection does not contain any null elements.)

If this collection makes any guarantees as to what order its elements are returned by its iterator, this method must return the elements in the same order.

Like the toArray method, this method acts as bridge between array-based and collection-based APIs. Further, this method allows precise control over the runtime type of the output array, and may, under certain circumstances, be used to save allocation costs

Suppose l is a List known to contain only strings. The following code can be used to dump the list into a newly allocated array of String:
code:
1
String[] x = (String[]) v.toArray(new String[0]);


Note that toArray(new Object[0]) is identical in function to toArray().

Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment


  • GiLuX
  • Registratie: Juni 1999
  • Laatst online: 12-11-2025
Op vrijdag 30 november 2001 21:05 schreef mbravenboer het volgende:
42
waarom weet jij alles?
beetje irritant hoor ;)

"I disagree with what you are saying, but I will defend to the death your right to say it." -- not clear who


  • tomato
  • Registratie: November 1999
  • Niet online
GiLuX: waarom weet jij alles?
Geloof het of niet, zelfs mbravenboer weet echt niet alles >:)
beetje irritant hoor ;)
Dat wel jah :P ;)

  • mbravenboer
  • Registratie: Januari 2000
  • Laatst online: 06-11-2025
GiLuX: waarom weet jij alles?
beetje irritant hoor ;)
Graag gedaan ;) .
tomato Geloof het of niet, zelfs mbravenboer weet echt niet alles
Tsss, gedraag je een beetje ;) . Maar goed, ik leer nog elke dag bij. Einde bewijs ;) .

Blog, Stratego/XT: Program Transformation, SDF: Syntax Definition, Nix: Software Deployment

Pagina: 1