Im little confuse about generic Arraylist. My professor did Public class ArrayLi
ID: 3749360 • Letter: I
Question
Im little confuse about generic Arraylist. My professor didPublic class ArrayListReview <N extends Number> { ArrayList<N> mylist=new ArrayList<>();
ArrayListReview(){ mylist=(ArrayList<N>) new ArrayList<>(Arrays.asList(new Integer[]{45,53,10}));
But I have no idea what is this about. Why she put <N> as an object and why not mylist.add(45) etc.
Thank you.. Create an ArrayListR eview class with one generic type to do the following Creates anarray list filled with numbers, and inserts new elements into the specified location in the list. Create a method inside the class to implement the calculation of Fibonacci numbers. Use System.nanoTime to find out how much time it takes to get fab(50). Create a method inside the class to check if an array list is a palindrome, assuming each element in the array list is one character.
Explanation / Answer
ArrayList<N> mylist=new ArrayList<>();
This ensures that arraylist contains only those elements which is of type N.In this case it is any whose super class is Number.
If we create an arraylist as ArrayList mylist=new ArrayList<>();, then it can hold item of any type.This is little weird in certain cases.So to ensure that an arraylist holds only those items which is of a particular type we create arrylist as ArrayList<N> mylist=new ArrayList<>();
---------------------------------------------------------------------------------------------------------------
ArrayListReview.java
import java.util.ArrayList;
/**
* ArrayList to hold generic type N (any type that extends Number)
* @author
* @param <N> any type that extends Number
*/
class ArrayListReview<N extends Number> {
ArrayList<N> mylist = new ArrayList<>();//an instance
//cretes an empty list
ArrayListReview() {
//mylist = (ArrayList<N>) new ArrayList<>(Arrays.asList(new Integer[]{45, 53, 10}));
}
/**
* insert an element at specified index
* @param num element to insert
* @param index index at which we need to insert
*/
public void insert(N num, int index) {
mylist.add(index, num);
}
/**
* prints the arraylist
*/
public void print() {
int i;
System.out.print("{ ");
if (mylist.size() == 0) {
System.out.print("List Empty.");
} else {
for (i = 0; i < mylist.size() - 1; i++) {
System.out.print(mylist.get(i) + " , ");
}
System.out.print(mylist.get(i));
}
System.out.println("} ");
}
/**
* Generate fibnocci numbers upto n.It clears all the contents of arraylist.
* Fibnocci numbers are those series in which a term is the sum of previous term
* Term at n = Term at n-1 +Term at n-2
* @param n length of series
*/
public void fibnocci(int n) {
mylist.clear();//clears arraylist to holds fibnocci numbers
long startTime=System.nanoTime();//get time in nanoseconds
long num1 = 0, num2 = 1;
if (n == 1) {
insert((N) (Number) num1, 0);
} else if (n == 2) {
insert((N) (Number) num1, 0);
insert((N) (Number) num2, 1);
} else {
insert((N) (Number) num1, 0);
insert((N) (Number) num2, 1);
for (int i = 2; i < n; i++) {
long sum = (Long) mylist.get(i - 1) + (Long) mylist.get(i - 2);
insert((N) (Number) (sum), i);
}
}
long endTime=System.nanoTime();
System.out.println("Time to generate "+n+" fibnocci numbers:"+(endTime-startTime)+" ns");
}
/**
* Checks if arraylist is palindrome or not
* Palindrome is a series in which is same when read from either end
* @return true if its a palindrome ,false otherwise
*/
public boolean isPalindrome() {
if (mylist.size() == 0) {
System.out.println("List Empty.");
}
for (int i = 0; i < mylist.size() / 2; i++) {
if (mylist.get(i) != mylist.get(mylist.size() - 1 - i)) {
return false;
}
}
return true;
}
}
-----------------------------------------------------------------------------------------------------------------------
TestArrayListReview .java
import java.util.Scanner;
/**
* A class to test ArrayListReview
* @author
*/
public class TestArrayListReview {
public static void main(String args[]){
int element;
int index;
int choice;
int n;
ArrayListReview al=new ArrayListReview();
Scanner in=new Scanner(System.in);
while(true)
{
System.out.println(" 1.Insert Element");
System.out.println("2.Print Array");
System.out.println("3.Generate fibnocci numbers");
System.out.println("4.Check if list palindrome");
System.out.println("5.Exit");
System.out.print("Enter your choice(1-5):");
choice=in.nextInt();
switch(choice){
case 1:
System.out.print("Enter element:");
element=in.nextInt();
System.out.print("Enter location within (0-"+al.mylist.size()+") :");
index=in.nextInt();
if(index>al.mylist.size())
System.out.print("Location should be within (0-"+al.mylist.size()+") :");
else
al.insert(element, index);
break;
case 2:
al.print();
break;
case 3:
System.out.println("Warning!! This will clear the existing list.");
System.out.print("Enter n(number of fibnocci numbers to generate):");
n=in.nextInt();
al.fibnocci(n);
System.out.println(" Fibnocci Series:");
al.print();
break;
case 4:
if(al.isPalindrome())
System.out.println(" List is a palindrome");
else
System.out.println(" List is not a palindrome");
break;
case 5:
System.exit(0);
default:
System.out.print("Enter correct choice(1-5)");
}
}
}
}
--------------------------------------------------------------------------------------------------------
Sample output
run:
1.Insert Element
2.Print Array
3.Generate fibnocci numbers
4.Check if list palindrome
5.Exit
Enter your choice(1-5):1
Enter element:1
Enter location within (0-0) :0
1.Insert Element
2.Print Array
3.Generate fibnocci numbers
4.Check if list palindrome
5.Exit
Enter your choice(1-5):1
Enter element:2
Enter location within (0-1) :1
1.Insert Element
2.Print Array
3.Generate fibnocci numbers
4.Check if list palindrome
5.Exit
Enter your choice(1-5):1
Enter element:2
Enter location within (0-2) :2
1.Insert Element
2.Print Array
3.Generate fibnocci numbers
4.Check if list palindrome
5.Exit
Enter your choice(1-5):1
Enter element:1
Enter location within (0-3) :3
1.Insert Element
2.Print Array
3.Generate fibnocci numbers
4.Check if list palindrome
5.Exit
Enter your choice(1-5):2
{ 1 , 2 , 2 , 1}
1.Insert Element
2.Print Array
3.Generate fibnocci numbers
4.Check if list palindrome
5.Exit
Enter your choice(1-5):4
List is a palindrome
1.Insert Element
2.Print Array
3.Generate fibnocci numbers
4.Check if list palindrome
5.Exit
Enter your choice(1-5):1
Enter element:3
Enter location within (0-4) :4
1.Insert Element
2.Print Array
3.Generate fibnocci numbers
4.Check if list palindrome
5.Exit
Enter your choice(1-5):4
List is not a palindrome
1.Insert Element
2.Print Array
3.Generate fibnocci numbers
4.Check if list palindrome
5.Exit
Enter your choice(1-5):3
Warning!! This will clear the existing list.
Enter n(number of fibnocci numbers to generate):50
Time to generate 50 fibnocci numbers:37873019 ns
Fibnocci Series:
{ 0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55 , 89 , 144 , 233 , 377 , 610 , 987 , 1597 , 2584 , 4181 , 6765 , 10946 , 17711 , 28657 , 46368 , 75025 , 121393 , 196418 , 317811 , 514229 , 832040 , 1346269 , 2178309 , 3524578 , 5702887 , 9227465 , 14930352 , 24157817 , 39088169 , 63245986 , 102334155 , 165580141 , 267914296 , 433494437 , 701408733 , 1134903170 , 1836311903 , 2971215073 , 4807526976 , 7778742049}
1.Insert Element
2.Print Array
3.Generate fibnocci numbers
4.Check if list palindrome
5.Exit
Enter your choice(1-5):4
List is not a palindrome
1.Insert Element
2.Print Array
3.Generate fibnocci numbers
4.Check if list palindrome
5.Exit
Enter your choice(1-5):5