Question

Implement a MyArrayList class similar to the java.util.ArrayList class using an array of objects. Although an...

Implement a MyArrayList class similar to the java.util.ArrayList class using an array of objects. Although an array

0 0
Add a comment Improve this question Transcribed image text
Answer #1

program

import java.util.*;
import java.util.Scanner;
import java.util.Collections;
public class MyArrayList {
public static void main(String args[]) {
// create an array list
Scanner sc = new Scanner(System.in);
ArrayList<Integer> arr=new ArrayList<Integer>(16);
ArrayList<Integer> duplicateArray=new ArrayList<Integer>(32);
for (int i = 0; i < 16; i++) {
System.out.println("Enter number");
int num=sc.nextInt();
arr.add(num);
  
if(arr.size()>15){
break;
}
}
//adding elements to duplicate array
if(arr.size()>=15){
duplicateArray.addAll(arr);
}
//adding
arr.add(10,5);

//contains
boolean res = arr.contains(5);
if (res == true) {
System.out.println("element 5 is contained in the list");
}
else {
System.out.println("element 5 is not contained in the list");
}

// 5th element value retrieval
int val=arr.get(5);
System.out.println("Retrieve element is = " + val);   
  
/*// getting index of 5
val=arr.IndexOf(5);
System.out.println("The number 5 at index " + val);*/
  
//checking empty or not
res = arr.isEmpty();
if (res == true) {
System.out.println("empty");
}
else {
System.out.println("not empty");
}
  
// getting index of value 5
val=arr.lastIndexOf(5);
System.out.println("The index of 5 is " + val);
  
//removes at index 5
arr.remove(5);
  
// Remove 5
arr.remove(5);

// setting 55 to number at 5
arr.set(5,55);
// display the array list
System.out.println("arr has: " + arr);
System.out.println("\nduplicate array has: " + duplicateArray);
}
}

E] Terminal Enter number 15 Enter number 16 element 5 is contained in the list Retrieve element is 6 not empty The last occur

Add a comment
Know the answer?
Add Answer to:
Implement a MyArrayList class similar to the java.util.ArrayList class using an array of objects. Although an...
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
  • An array of class objects is similar to an array of some other data type. To...

    An array of class objects is similar to an array of some other data type. To create an array of Points, we write Point parray [4]; To access the object at position i of the array, we write parray [i] and to call a method on that object method, we write parray [i]. methodName (arg1 , arg2 , ...) ; To initialize an array of objects whose values are known at compile time, we can write Point parray [4] =...

  • Design and implement a Java data structure class named BookShelf which has an array to store...

    Design and implement a Java data structure class named BookShelf which has an array to store books and the size data member. The class should have suitable constructors, get/set methods, and the toString method, as well as methods for people to add a book, remove a book, and search for a book. Design and implement a Java class named Book with two data members: title and price. Test the two classes by creating a bookshelf object and five book objects....

  • Implement a class of char Queue using the concept of array with the following member functions:...

    Implement a class of char Queue using the concept of array with the following member functions: push, pop (void), peek, empty, and full as explained in the lecture. In C++

  • JAVA I. Using the Event Class created previously, create an array of objects;        II. Demonstrate...

    JAVA I. Using the Event Class created previously, create an array of objects;        II. Demonstrate passing array reference to a method;        III. Demonstrate creating array of objects that prints out only x 0.0 for all objects. PROJECT 5C - ARRAYS Create a new file called " EventArray5C". There are some "challenging" directions in this project. . 1.Prepare a document box (tell me that task(s) the application is to accomplish, how it will accomplish the tasks, the author of...

  • In Java, Implement a class MyArray as defined below, to store an array of integers (int)....

    In Java, Implement a class MyArray as defined below, to store an array of integers (int). Many of its methods will be implemented using the principle of recursion. Users can create an object by default, in which case, the array should contain enough space to store 10 integer values. Obviously, the user can specify the size of the array s/he requires. Users may choose the third way of creating an object of type MyArray by making a copy of another...

  • **Please do it in C++*** The university roster will be stored using an array list (class...

    **Please do it in C++*** The university roster will be stored using an array list (class StudentList). StudentList works as a regular arrayList, but instead of being a template class, it can only hold Student objects. We design it this way because some operations of StudentList would not be applicable to other objects. The StudentList class has the following attributes: int size int length Student *list (pointer for dynamic array of students) Complete all the methods of studentList.h to be...

  • In Java: Executable Class create an array of Employee objects. You can copy the array you...

    In Java: Executable Class create an array of Employee objects. You can copy the array you made for Chapter 20. create an ArrayList of Employee objects from that array. use an enhanced for loop to print all employees as shown in the sample output. create a TreeMap that uses Strings for keys and Employees as values. this TreeMap should map Employee ID numbers to their associated Employees. process the ArrayList to add elements to this map. print all employees in...

  • Equals and Copy method activity Implement a Dog class with the following features: + Attributes: ...

    Java Equals and Copy method activity Implement a Dog class with the following features: + Attributes: age, gender, weight + Method: constructor, copy constructor, equals (if same weight and gender), toString + A main method/driver that create an array of 5 Dog objects (obtain input from user), and then display the objects that are "equal" Equals and Copy method activity Implement a Dog class with the following features: + Attributes: age, gender, weight + Method: constructor, copy constructor, equals (if...

  • Assessment O Submissions.. l import java.util.List; 2 import java.util.Arraylist; 4 public class ArrayHeapChecker 7Checks if the...

    Assessment O Submissions.. l import java.util.List; 2 import java.util.Arraylist; 4 public class ArrayHeapChecker 7Checks if the given array is a representation of a binary tree * @param entries 10 array of entries to be test * ereturn true if the input array encodes a binary tree, false otherwise 12 13 14 public static <K extends Comparable K, vs boolean isBinaryTree(List Entry<k,v entries) ( 15 16 17 // TODO: implement this return true 18 19 2e 21 * Checks if the...

  • Create and implement a C++ class, RandomGenerator, that: Allocates and stores an array of random floating...

    Create and implement a C++ class, RandomGenerator, that: Allocates and stores an array of random floating point numbers between 0 and 1. The size of the array is passed into the constructor. The class contains a next() function which returns a new random number from the array every time the next() function is called (without calling rand()). Deletes any memory allocated on the heap when the class is destroyed. Note: This trick pre-calculates random numbers and saves execution time by...

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