Question

Language: Java when creating a method to add to the front of the arraylist, I have...

Language: Java

when creating a method to add to the front of the arraylist, I have the following:

public void addToFront(T data) {
   if (data == null) {
       throw new IllegalArgumentException("Data here is null");
   }
   for (int i = size; i > 0; i--) {
       backingArray[i] = backingArray[i -1];
   }
   size++;
}

I was wondering what the bold part of this code means. Thanks!

Homework Answers

Answer #1
for (int i = size; i > 0; i--) {
    backingArray[i] = backingArray[i -1];
}
size++;

This means moving the content of array backingArray to one location right to make the front of the array available to insert new element.
And then it increments the size by 1.

Example:
If the array looks like
4 6 3 7 8
After the above block execution it looks like
   4 6 3 7 8
Means all the elements are shifted to one location right to make first location available.
Know the answer?
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
Similar Questions
Language: Java Topic: Deques Using the following variables/class: public class ArrayDeque<T> { /** * The initial...
Language: Java Topic: Deques Using the following variables/class: public class ArrayDeque<T> { /** * The initial capacity of the ArrayDeque. * * DO NOT MODIFY THIS VARIABLE. */ public static final int INITIAL_CAPACITY = 11; // Do not add new instance variables or modify existing ones. private T[] backingArray; private int front; private int size; Q1: write a method that constructs a new arrayDeque called "public ArrayDeque()" Q2: write a method called "public void addFirst(T data)" that adds an element...
Language: Java Topic: Deques Using the following variables/class: public class ArrayDeque<T> { /** * The initial...
Language: Java Topic: Deques Using the following variables/class: public class ArrayDeque<T> { /** * The initial capacity of the ArrayDeque. * * DO NOT MODIFY THIS VARIABLE. */ public static final int INITIAL_CAPACITY = 11; // Do not add new instance variables or modify existing ones. private T[] backingArray; private int front; private int size; Q1: write a method called "public void addLast(T data)" which adds an element to the back of a Deque. If sufficient space is not available...
Java Language Add a method (convertToRing ()) that converts the list to a circular. This is,...
Java Language Add a method (convertToRing ()) that converts the list to a circular. This is, which makes the last node instead of pointing to null point to the first. Code: class Node { int value; Node nextNode; Node(int v, Node n) { value = v; nextNode = n; } Node (int v) { this(v,null); } } class LinkedList { Node head; //head = null; LinkedList() { } int length() { Node tempPtr; int result = 0; tempPtr = head;...
Java Language Add a method (convertToRing ()) that converts the list to a circular. This is,...
Java Language Add a method (convertToRing ()) that converts the list to a circular. This is, which makes the last node instead of pointing to null point to the first. Code: class Node { int value; Node nextNode; Node(int v, Node n) { value = v; nextNode = n; } Node (int v) { this(v,null); } } class LinkedList { Node head; //head = null; LinkedList() { } int length() { Node tempPtr; int result = 0; tempPtr = head;...
Complete the redblacktree in Java. Add a public boolean isBlack field to the Node inner class....
Complete the redblacktree in Java. Add a public boolean isBlack field to the Node inner class. Make every Node object have a false isBlack field, all new node is red by default. In the end of the insert method, set the root node of your red black tree to be black. Implement the rotate() and recolor() functions, and create tests for them in a separate class. import java.util.LinkedList; public class BinarySearchTree<T extends Comparable<T>> { protected static class Node<T> { public...
In the attached FlexArray Java class, implement a method public int delete (int location) { }...
In the attached FlexArray Java class, implement a method public int delete (int location) { } that deletes the integer value stored at location in the array, returns it, and ensures that the array values are contiguous.  Make sure to handle the array empty situation.  What is the time-complexity of the method, if the array size is n. ***************************************************************************************************************************** public class FlexArray { int [] array; private int size; private int capacity; public FlexArray() { capacity=10; size=0; array=new int[10]; } public FlexArray(int...
Language: Java Topic: Deques Using the following variables/class: public class LinkedDeque<T> { // Do not add...
Language: Java Topic: Deques Using the following variables/class: public class LinkedDeque<T> { // Do not add new instance variables or modify existing ones. private LinkedNode<T> head; private LinkedNode<T> tail; private int size; Q1: write a method called "public T removeFirst()" that does the following: * Removes and returns the first element of the deque. * Must be in O(1) * @return the data formerly located at the front of the deque * @throws java.util.NoSuchElementException if the deque is empty Q2:...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {    public static final int DEFAULT_SIZE = 10;    private Object data[];    private int index; code 2 package test; import java.util.*; /* Class Node */ class Node { protected Object data; protected Node link; /* Constructor */ public Node() { link = null; data = 0; } /* Constructor */ public Node(Object d,Node n) { data = d; link = n; } /*...
Add a method to CatList with this header public ArrayList getCatsSortedByName() which sorts the inner ArrayList...
Add a method to CatList with this header public ArrayList getCatsSortedByName() which sorts the inner ArrayList of cats and returns it. Use an anonymous inner class which implements Comparator. public static void main(String[] args) { CatList cats = new CatList(); System.out.println("will create the cats"); cats.addCat(new Cat("Frank", "gray")); cats.addCat(new Cat("Zen", "black")); cats.addCat(new Cat("Xiongmao", "spotted")); cats.addCat(new Cat("Little Kit", "calico")); cats.addCat(new Cat("Ang", "gray")); System.out.println("Here is the list"); System.out.println(cats); System.out.println("There are " + cats.howManyCats() + "cats on the list");       //ArrayList<Cat> sortedCats =...
Java program question! If I pass null to a method, how can I use the passed...
Java program question! If I pass null to a method, how can I use the passed value to compare. Here is example code, it might be wrong, please make it could run on eclipse. Public class hw{ public static void main (String args[]) { method1(); } private static void method1() { System.out.println(method(null)); } public static String method(int[] a) { return null; } What I want to get in the output is just "()". It is not just to type the...