Java Programming:
Task:
- Write some Java code that creates a collection (an ArrayList, a TreeSet or a LinkedList), puts some elements into it, creates an iterator, uses it a bit and then (1) changes the collection using one of the four techniques listed (not using the first iterator) and then (2) calls one of the three iterator methods listed. (See instructions below for details)
- After writing the code answer the following questions:
1.) What is the result?
2.) Is a concurrent modification exception thrown?
3.) Why or why not?
Instructions:
For this discussion we will consider the following
methods:
* clear
* add
* remove (in the collection class)
* remove (in the iterator class)
An iterator also has three methods:
* hasNext
* next
* remove
The code for this experiment is listed at the end.
Test:
In this test, we have created an array list of integers, added numbers between 1 to 10. Created an Iterator object, printed first three numbers using next() method of the iterator, and then made changes to the original list using clear() method of the array list. Finally we called the next() method of iterator again.
Observations after conducting the experiment:
The final call to next() method of iterator class caused in ConcurrentModificationException. This is because Java does not allow modification on the original collection when it is being iterated. Suppose you are iterating through a collection using iterator, then if the original list is mutated, the iterator methods won’t work correctly as it is mapped to the orginal list during the iterator object creation. But iterator provides a remove() method, which can be used to remove the last iterated value from the original list, keeping iteration process uninterrupted. However, any method calls on the orginal collection, that will mutate the original collection’s order, like add() or remove() will result in violating the modification rule, and cause the iterator methods to fail.
// Test.java
import java.util.ArrayList;
import java.util.Iterator;
public class Test {
public static void main(String[] args) {
// creating an ArrayList of integers
ArrayList<Integer> list = new ArrayList<Integer>();
// adding numbers between 1 and 10 into the list
for (int i = 1; i <= 10; i++) {
list.add(i);
}
// getting an iterator from the list
Iterator<Integer> iterator = list.iterator();
// printing first three numbers using iterator
System.out.println(iterator.next());// 1
System.out.println(iterator.next());// 2
System.out.println(iterator.next());// 3
// now making changes to the original list. be it any mutating operation
list.clear();
// now trying to access next element of iterator.
System.out.println(iterator.next());
}
}
/*OUTPUT*/
1
2
3
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(Unknown Source)
at java.util.AbstractList$Itr.next(Unknown Source)
at Test.main(Test.java:22)
Java Programming: Task: - Write some Java code that creates a collection (an ArrayList, a TreeSet...
JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class. Print out the results after testing each of the methods in both of the classes and solving a simple problem with them. Task 1 – ArrayList Class Create an ArrayList class. This is a class that uses an internal array, but manipulates the array so that the array can be dynamically changed. This class should contain a default and overloaded constructor, where the default...
Java implement the method in IteratorExercise.java using only list iterator methods: bubbleSort: sort the provided list using bubble sort Do not modify the test code in each function. You can look at that code for some ideas for implementing the methods. import java.lang.Comparable; import java.util.*; public class IteratorExercise { public static <E extends Comparable<? super E>> void bubbleSort(List<E> c) throws Exception { // first line to start you off ListIterator<E> iit = c.listIterator(), jit;...
Array with Iterator. Java style
Implement an array data structure as a class JstyArray<E>
to support the Iterable interface such that the following code
works:
JstyArray<Integer> data;
data = new JstyArray<Integer>(10);
for (int i = 0; i < 10; ++i) {
data.set(i, new Integer(i) );
}
int sum = 0;
for ( int v : data ) {
if (v == null)
continue; // empty cell
sum += v;
}
The iterator provided by this class follows the behaviour of...
Please code this in java: The purpose of this assignment is to: Read double data in to a TreeSet where each datum inserted is Math.abs(datum) (i.e., no negative values are inserted). NOTE: This will (i) sort the data placed into the TreeSet, and, (ii) remove duplicate values since no duplicates are allowed (i.e., duplicates will not be inserted). Create a list iterator. Iterate backwards through the TreeSet (i.e., from the largest value to the smallest) outputting all elements * -1.0....
Java Task 2: Write a program where you create an ArrayList object that can store letters. Show how can you add a letter to the ArrayList, remove a letter from theArrayList, replace a letter in the ArrayList, insert a letter in a specific location in the ArrayList, and display all the letters in the ArrayList. Task 3: Write a program where you create an ArrayList object that can store Circle object (you created circle class in your previous PAs). Create...
how would I complete this code without calling any built-in java collection framework classes like ArrayList, LinkedList, etc? import java.util.Iterator; class CallStack<T> implements Iterable<T> { // You'll want some instance variables here public CallStack() { //setup what you need } public void push(T item) { //push an item onto the stack //you may assume the item is not null //O(1) } public T pop() { //pop an item off the stack //if there are no items on the stack, return...
Write some Java code representing a task from real life, and include a loop and/or collection. Example: for (ClothingItem item : laundry) { washer.add(item); } washer.setFillLevel("max"); washer.setTemperature("warm"); washer.wash();
Java / Generic Programming When an iterator is created, what element will be removed if the "remove" method is called right away? * The first one * None-remove is not allowed to be called right way * None-remove will throw a NoSuchElementException. * The last one. What is an advantages of using "enhanced for loops" rather than explicitly using iterators? * It generates faster code. * It can handle generic containers. * It doesn't misbehave if the collection changes during...
Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to both classes: public void reverseThisList(), This method will reverse the lists. When testing the method: print out the original list, call the new method, then print out the list again ------------------------------------------------------------------------- //ARRAY LIST class: public class ArrayList<E> implements List<E> { /** Array of elements in this List. */ private E[] data; /** Number of elements currently in this List. */ private int size; /**...
The following is for java programming. the classes
money date and array list are so I are are pre made to help with
the coding so you can resuse them where applicable
Question 3. (10 marks) Here are three incomplete Java classes that model students, staff, and faculty members at a university class Student [ private String lastName; private String firstName; private Address address; private String degreeProgram; private IDNumber studentNumber; // Constructors and methods omitted. class Staff private String lastName;...