need help with my PersonArrayList greaterThan(Person p) – returns a PersonArrayList object containing Person objects that are greater than p
public class PersonArrayList<E> {
private Person[] list;
private int size = 0;
PersonArrayList(int capacity) {
this.list = new Person[capacity];
}
private void ensureCapacity(int minCapacity) {
int oldCapacity = list.length;
int newCapacity = oldCapacity + (oldCapacity >> 1);
if (newCapacity - minCapacity < 0) {
newCapacity = minCapacity;
}
list = Arrays.copyOf(list, newCapacity);
}
void add(Person p) {
if (size >= list.length) {
ensureCapacity(list.length);
list[size] = p;
size++;
}
}
void add(int index, Person value) {
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException();
}
if (size >= list.length) {
ensureCapacity(list.length);
list[index] = value;
size++;
}
}
Person remove(int index) {
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException();
}
for (int i = 0; i < size - 1; i++) {
list[i] = list[i + 1];
}
list[size - 1] = null;
return list[size];
}
boolean remove(Person s) {
boolean flag = false;
for (int i = 0; i < size && !flag; i++) {
if (list[i].equals(s)) {
remove(i);
flag = true;
}
}
return flag;
}
Person get(int index) {
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException();
}
return list[index];
}
int size() {
return size;
}
PersonArrayList greaterThan(Person p) {
PersonArrayList temp = new PersonArrayList(p);
for (int i = 0; i < size; i++) {
if (list[i].compareTo(p) > 0) {
temp = p;
add(p);
}
}
return temp;
}
Person[] sort() {
Person[] temp = new Person[size];
System.arraycopy(list, 0, temp, 0, size);
Arrays.sort(temp);
return temp;
}
}
CODE
import java.util.Arrays;
public class PersonArrayList<E> {
private Person[] list;
private int size = 0;
PersonArrayList(int capacity) {
this.list = new Person[capacity];
}
private void ensureCapacity(int minCapacity) {
int oldCapacity = list.length;
int newCapacity = oldCapacity + (oldCapacity >> 1);
if (newCapacity - minCapacity < 0) {
newCapacity = minCapacity;
}
list = Arrays.copyOf(list, newCapacity);
}
void add(Person p) {
if (size >= list.length) {
ensureCapacity(list.length);
list[size] = p;
size++;
}
}
void add(int index, Person value) {
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException();
}
if (size >= list.length) {
ensureCapacity(list.length);
list[index] = value;
size++;
}
}
Person remove(int index) {
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException();
}
for (int i = 0; i < size - 1; i++) {
list[i] = list[i + 1];
}
list[size - 1] = null;
return list[size];
}
boolean remove(Person s) {
boolean flag = false;
for (int i = 0; i < size && !flag; i++) {
if (list[i].equals(s)) {
remove(i);
flag = true;
}
}
return flag;
}
Person get(int index) {
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException();
}
return list[index];
}
int size() {
return size;
}
PersonArrayList<E> greaterThan(Person p) {
int count = 0;
for (int i = 0; i < size; i++) {
if (list[i].compareTo(p) > 0) {
count ++;
}
}
PersonArrayList<E> temp = new PersonArrayList<>(count);
for (int i = 0; i < size; i++) {
if (list[i].compareTo(p) > 0) {
temp.add(p);
}
}
return temp;
}
Person[] sort() {
Person[] temp = new Person[size];
System.arraycopy(list, 0, temp, 0, size);
Arrays.sort(temp);
return temp;
}
}
NOTE: In order to run this code, your Person class should implement Comparable and should override the compareTo(). Since you have not provide the PERSON class, I am assuming that it has overridden the compareTo().
need help with my PersonArrayList greaterThan(Person p) – returns a PersonArrayList object containing Person objects that...
PLEASE EDIT THE LAST 3 METHODS WHERE I PUT STARS AND DO WHAT THE COMMENTS SAY. THE CODE IS IN JAVA PROGRAMMING LANGUAGE. import java.util.AbstractList; import java.util.List; import java.util.RandomAccess; import java.lang.RuntimeException; import java.util.Arrays; public class Vector<E> extends AbstractList<E> implements List<E>, RandomAccess { protected Object[] data; protected int size; public int size() { return size; } private void rangeCheck(int index) { if (index < 0 || index >= size) throw new IndexOutOfBoundsException(""); } @SuppressWarnings("unchecked") private E...
Java Programming: The following is my code: public class KWSingleLinkedList<E> { public void setSize(int size) { this.size = size; } /** Reference to list head. */ private Node<E> head = null; /** The number of items in the list */ private int size = 0; /** Add an item to the front of the list. @param item The item to be added */ public void addFirst(E...
Improve the speed of public T get(int i) by having it work backward from the end of the array if you attempt to get a member which is closer to the end than the start. This improvement will be tested through timing tests on large lists. The method should still return null if i is not a valid index. Code import java.util.Iterator; public class DLList<T> implements Iterable<T> { private static class Node<T> { public Node<T> prev, next;...
JAVA // TO DO: add your implementation and JavaDoc public class SmartArray<T>{ private static final int DEFAULT_CAPACITY = 2; //default initial capacity / minimum capacity private T[] data; //underlying array // ADD MORE PRIVATE MEMBERS HERE IF NEEDED! @SuppressWarnings("unchecked") public SmartArray(){ //constructor //initial capacity of the array should be DEFAULT_CAPACITY } @SuppressWarnings("unchecked") public SmartArray(int initialCapacity){ // constructor // set the initial capacity of...
Java Programming: The following is my code: import java.util.Arrays; public class KWArrayList<E> { // Data fields /** The default initial capacity */ private static final int INITIAL_CAPACITY = 10; /** The underlying data array */ private E[] theData; /** The current size */ private int size = 0; /** The current capacity */ private int capacity = 0; @SuppressWarnings("unchecked") public KWArrayList() { capacity...
JAVA: Already completed: MyList.java, MyAbstractList.java, MyArrayList.java, MyLinkedLink.java, MyStack.java, MyQueue.java. Need to complete: ReversePoem.java. This program has you display a pessimistic poem from a list of phrases. Next, this program has you reverse the phrases to find another more optimistic poem. Use the following algorithm. 1. You are given a list of phrases each ending with a pound sign: ‘#’. 2. Create a single String object from this list. 3. Then, split the String of phrases into an array of phrases...
Create a complete LinkedList class which implements all of the methods listed below using dynamic memory allocation. You will need a Node class to represent each node in the list. The list should store String values. Include a main method which tests all the methods of your list. Also answer these questions: What is the time complexity (using big-O notation) of these operations in your linked list? add(String val) add(int index, String val) get(int index) remove(String val) Submit three files:...
Currently working on a Java Assignment. I have written most codes for swap, reverse and insert. Just need a. itemCount receives a value and returns a count of the number of times this item is found in the list. c. sublist receives two indexes and returns an ArrayList of node values from the first index to the second index, provided the indexes are valid. d. select receives a variable number of indexes, and returns an ArrayList of node values corresponding...
Please help with the codes for the implementation of java.util.List, specifically for the 3 below overridden methods @Override public int lastIndexOf(Object arg0) { required code } @Override public ListIterator<R> listIterator() { required code } @Override public ListIterator<R> listIterator(int arg0) { required code } PLEASE NOTE: Below are some of other overridden methods that are already implemented, they are meant to be examples of what the answers to the above questions for the 3 methods should...
I need help with todo line please public class LinkedList { private Node head; public LinkedList() { head = null; } public boolean isEmpty() { return head == null; } public int size() { int count = 0; Node current = head; while (current != null) { count++; current = current.getNext(); } return count; } public void add(int data) { Node newNode = new Node(data); newNode.setNext(head); head = newNode; } public void append(int data) { Node newNode = new Node(data);...