Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

139
Views
Método Java PriorityQueue initElementsFromCollection

Tengo dificultades para digerir este bloque de código en particular del método java.util.PriorityQueue#initElementsFromCollection .

 /** * Initializes queue array with elements from the given Collection. * * @param c the collection */ private void initFromCollection(Collection<? extends E> c) { initElementsFromCollection(c); heapify(); } private void initElementsFromCollection(Collection<? extends E> c) { Object[] es = c.toArray(); int len = es.length; if (c.getClass() != ArrayList.class) es = Arrays.copyOf(es, len, Object[].class); if (len == 1 || this.comparator != null) for (Object e : es) if (e == null) throw new NullPointerException(); this.queue = ensureNonEmpty(es); this.size = len; }

Puedo entender que el código aquí intenta construir el montón a partir de los elementos de la colección proporcionados a través del constructor, pero ¿por qué están comprobando el tipo de clase del argumento de la Collection contra ArrayList y copiando nuevamente los elementos, ya se ha copiado en Object[] es usando toArray ?

 if (c.getClass() != ArrayList.class) es = Arrays.copyOf(es, len, Object[].class);

¿Sucede algo mágico en java.util.Arrays#copyOf(U[], int, java.lang.Class<? extends T[]>) ?

java-11

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!