Question

Could you please explain, how this code works? public void addAfter(int element){ if (this.manyItems == 0){...

Could you please explain, how this code works?

public void addAfter(int element){

if (this.manyItems == 0){

this.data[current] = element; this.manyItems++;

}

else

{

for (int i=this.manyItems-1; i > this.current; i--)

this.data[i+1] = this.data[i];

this.current++;

this.data[current] = element; this.manyItems++; }

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
1. 
If number of items in the data array is zero then adding element at position current
and then incrementing manyItems by 1.

2.
Otherwise shifting each element by one position and then adding element at current,
and then incrementing manyItems by 1.

Like data in data array is 1,2,3,4,5
If we want to add element 6 then we shift each element by 1 then the data array looks like below
<empty>,1,2,3,4,5
Then we add new element at empty position
So, new array becomes
6,1,2,3,4,5
Add a comment
Know the answer?
Add Answer to:
Could you please explain, how this code works? public void addAfter(int element){ if (this.manyItems == 0){...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • import java.util.Arrays; public class lab {    public static void main(String args[])    {    int...

    import java.util.Arrays; public class lab {    public static void main(String args[])    {    int arr[] = {10, 7, 8, 9, 1, 5,6,7};    int arr2[] = {9, 8, 7, 6, 5, 4, 3, 2, 1};    int arr3[] = {1, 3, 5, 3, 2, 6, 20};    quicksort(arr,0,arr.length-1);    quicksort(arr2,0,arr2.length-1);    quicksort(arr3,0,arr3.length-1);    System.out.println(Arrays.toString(arr));    System.out.println(Arrays.toString(arr2));    System.out.println(Arrays.toString(arr3));       }    private static int partition(int[] items,int low, int high)    {    int i=0;    int j=0;...

  • Fix the errors in C code #include <stdio.h> #include <stdlib.h> void insertAt(int *, int); void Delete(int...

    Fix the errors in C code #include <stdio.h> #include <stdlib.h> void insertAt(int *, int); void Delete(int *); void replaceAt(int *, int, int); int isEmpty(int *, int); int isFull(int *, int); void removeAt(int *, int); void printList(int *, int); int main() { int *a; int arraySize=0,l=0,loc=0; int choice; while(1) { printf("\n Main Menu"); printf("\n 1.Create list\n 2.Insert element at particular position\n 3.Delete list.\n4. Remove an element at given position \n 5.Replace an element at given position\n 6. Check the size of...

  • Explain this java code, please. import java.util.Scanner; public class Program11 { public static void main(String[] args)...

    Explain this java code, please. import java.util.Scanner; public class Program11 { public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); final int maxSize = 128; String[] titles = new String[maxSize]; int[] lengths = new int[maxSize]; int numDVDs = 0; String op; op = menu(stdIn); System.out.println(); while (!op.equalsIgnoreCase("q")) { if (op.equalsIgnoreCase("a")) { if (numDVDs < maxSize) numDVDs = addDVD(titles, lengths, numDVDs, stdIn); } else if (op.equalsIgnoreCase("t")) searchByTitle(titles, lengths, numDVDs, stdIn);    else if (op.equalsIgnoreCase("l")) searchByLength(titles, lengths, numDVDs, stdIn); System.out.println('\n');...

  • Explain in detail what the code below does: public class MyClass {       public static void...

    Explain in detail what the code below does: public class MyClass {       public static void main(String args[]) {              System.out.println(isUniqueChars("something"));       }       public static boolean isUniqueChars(String str) {             int checker = 0;                                                                                               for (int i = 0; i < str.length(); ++i) {                         int val = str.charAt(i) - 'a';                         if ((checker & (1 << val)) > 0) return false;                         checker |= (1 << val);             }             return true;...

  • Java i know that result will be 415. Can you explain how it works. step by...

    Java i know that result will be 415. Can you explain how it works. step by step please. thanks public static void whatsPrinted(int A[]) { for (int i=1; i<A.length; i++) { A[i]=A[i-1]*2+1; } } public static void main(String args[]) { int A[] = {12,3,8,9,7,11}; whatsPrinted(A); System.out.println(A[A.length-1]); }

  • must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int...

    must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int [] arr); public static void quickSort(int [] arr); public static void mergeSort(int [] arr); The quick sort and merge sort must be implemented by using recursive thinking. So the students may provide the following private static methods: //merge method //merge two sorted portions of given array arr, namely, from start to middle //and from middle + 1 to end into one sorted portion, namely,...

  • What output is produced by the following code? Explain how it works. 1.1 1 ...2..... 5...

    What output is produced by the following code? Explain how it works. 1.1 1 ...2..... 5 package taesik; import java.util.; /* Your name: Date: Answer: How it works: */ class student String name; int age; 1 public class Quiz21 ( public static void main(String args[])! ArrayList<student>myArray = new ArrayListo(); for(int i=0; i< 6; i++) { student s = new student(); s.name="Emily" ; 3.ageri; myArray.add(s); 1 for (int i = 0; i <myarray.size(); i++) { System.out.format($s $den", myArray.get(i).name, myArray.get(i)-age); } System.out.println();...

  • Java will be printed 10. can you explain step by step why? public class WhatsPrinted2 {...

    Java will be printed 10. can you explain step by step why? public class WhatsPrinted2 { public static void whatHappens(int A[]) { int []B = new int[A.length]; for (int i=0; i<A.length; i++) { B[i]=A[i]*2; } A=B; } public static void main(String args[]) { int A[] = {10,20,30}; whatHappens(A); System.out.println(A[0]); } } will print 10. explain how it's works. Thanks public class WhatsPrinted3 { public static int[] whatHappens(int A[]) { int []B = new int[A.length]; for (int i=0; i<A.length; i++) {...

  • Can you explain how this code for the 8 Queens Problem works? It's in C++ In case you dont know w...

    Can you explain how this code for the 8 Queens Problem works? It's in C++ In case you dont know what the 8 Queens problem is, "The eight queens puzzle is the problem of placing eightchess queens on an 8×8 chessboard so that no two queensthreaten each other; thus, a solution requires that no two queens share the same row, column, or diagonal." This is the code: #include <iostream> #include <cmath> using namespace std; bool ok(int q[], int c) {     ...

  • Convert into pseudo-code for below code =============================== class Main {    public static void main(String args[])...

    Convert into pseudo-code for below code =============================== class Main {    public static void main(String args[])    {        Scanner s=new Scanner(System.in);        ScoresCircularDoubleLL score=new ScoresCircularDoubleLL();        while(true)        {            System.out.println("1--->Enter a number\n-1--->exit");            System.out.print("Enter your choice:");            int choice=s.nextInt();            if(choice!=-1)            {                System.out.print("Enter the score:");                int number=s.nextInt();                GameEntry entry=new GameEntry(number);   ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT