Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi, could you please a make method that adds /list.add(0,Juliet);.I tried to do as you will see the bottom of this page. Please correct me

Hi, could you please a make method that adds /list.add(0,"Juliet");.I tried to do as you will see the bottom of this page. Please correct me what I had make mistake and write some details for me thanks.

1 //arryList (Chapter 4) 2 publicclassMyarraylist 3 { 4 privateObject[] buffer; 5 privateintcurrentSize; 6 publicMyarraylist() 7 { 8 finalintINITIAL_SIZE= 10; 9 buffer = newObject[INITIAL_SIZE]; 10 currentSize = 0; 11 } 12 publicintsize() { 13 returncurrentSize;} 14 15 16 privatevoidcheckBounds(intn) 17 { 18 if(n < 0 || n>= currentSize) 19 { 20 thrownewIndexOutOfBoundsException(); 21 } 22 } 23 24 publicObject get (intpos) 25 { 26 checkBounds(pos); 27 returnbuffer[pos]; 28 } 29 publicvoidset(intpos, Object element) 30 { 31 checkBounds(pos); 32 buffer[pos] = element; 33 } 34 publicbooleanaddLast(Object newElement) 35 { 36 growBufferIfNecessary(); 37 currentSize++; 38 buffer[currentSize - 1] = newElement; 39 returntrue; 40 } 41 publicbooleanaddFirst(Object newElement) 42 { 43 growBufferIfNecessary(); 44 currentSize++; 45 buffer[currentSize + 1] = newElement; 46 returntrue; 47 } 48 49 50 privatevoidgrowBufferIfNecessary() 51 { 52 if(currentSize == buffer.length) 53 { 54 Object[] newBuffer = 55 newObject[2 * buffer.length]; 56 for(inti = 0; i< buffer.length;i++) 57 { 58 newBuffer[i] = buffer[i]; 59 } 60 buffer= newBuffer; 61 } 62 } 63 publicObject remove(intpos) 64 { 65 checkBounds(pos); 66 Object removed = buffer[pos]; 67 for(inti = pos +1; i < currentSize;i++){ 68 buffer[i-1] = buffer[i]; 69 } 70 currentSize--; 71 returnremoved; 72 } 73 74 publicstaticvoidmain(String[]args){ 75 Myarraylist list = newMyarraylist(); 76 list.addLast("king"); 77 list.addLast("Queen"); 78 list.addLast("Pawn"); 79 list.addLast("Horse"); 80 //list.addFirst(0,"Juliet"); 81 System.out.println(list.size()); 82 list.remove (1); 83 for(inti=0; i

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Management System MCQs Multiple Choice Questions And Answers

Authors: Arshad Iqbal

1st Edition

1073328554, 978-1073328550

More Books

Students also viewed these Databases questions

Question

3 How the market system answers four fundamental questions.

Answered: 1 week ago

Question

5 The mechanics of the circular flow model.

Answered: 1 week ago