Anzeige
Adding elements to an Array- Given the following simple List class tha.docx
Adding elements to an Array- Given the following simple List class tha.docx
Nächste SlideShare
Adria Company recently implemented an activity-based costing system- A.docxAdria Company recently implemented an activity-based costing system- A.docx
Wird geladen in ... 3
1 von 2
Anzeige

Más contenido relacionado

Más de wviola(20)

Anzeige

Adding elements to an Array- Given the following simple List class tha.docx

  1. Adding elements to an Array. Given the following simple List class that uses an array implementation, write code to add an element at the specified index. Solution //----------------------------------------------------------------- // Adds the specified Comparable element to the list, //----------------------------------------------------------------- public void add (int index,T element) { If(index < 0 || index > rear) throw new IndexOutOfBoundsException(); if (rear == list.length) expandCapacity( ); Comparable<T> temp = (Comparable<T>)element; int scan = 0; while (scan < rear && temp.compareTo(list[scan]) > 0) scan++; for (int scan2=rear; scan2 > scan; scan2--) list[scan2] = list[scan2-1] list[index] = element;//add element in speciefied index rear++;
  2. }
Anzeige