Java Generics programming is introduced in J2SE 5 for dealing with type-safe objects. Before generics, one was able to store any type of objects in the collection which was non-generic. Now with generics, Java programmers are forced to store the specific type of objects. Several abstract data types represent a linear sequence of elements but with general support for removing or adding elements at different arbitrary positions. Designing a simple abstraction is well suited for proper implementation with an array or linked list which can be challenging as there are different nature of data structures.
Various locations within an array can be described by an integer index. An index of an element e in a particular sequence is equal to the number of elements e present in that sequence. The first elements of the sequence have index 0 while the last has index n-1 given the fact that n is the total number of elements. The concept of element’s index is defined for a linked list but it is not such a convenient notion as there is no fixed way to access an element at a given index without traversing a portion of a linked list which depends right on the magnitude of the index.
Java defined an interface, java.util.List, that which will include the following index-based methods:
Indexing of an existing element may change certainly over time as with other elements which are added or removed right in front of it. There are valid ranges for adding method which will include the current size of the list in the case new elements becomes the last elements.
There is the generics framework which is introduced in Java SE 5.0 and is updated in Java SE7 providing support which will allow different parameterization of the types.
The benefit of generics is a very significant reduction in the amount of code which is needed to be written when one is developing a library. There is certainly one more benefit that is eliminating the casting in various situations. There are classes of Collections Framework, the class Class and various other Java libraries which have been updated for including the generics.
Right in generic classes and interfaces, parameterization of types can happen by adding a type parameter well within the angular brackets (i.e.,). The type is present at the place of the brackets. Right once it is instantiated, there is generic parameter type which is applied right throughout the class for the various methods which have the same type which is specified. Take the following example, the add() and get() methods which will use parameterized type right as their parameter argument and return types respectively:
public interface List extends Collection{ public boolean add(E e); E get(int index); }
When a variable of a parameterized type is declared, there is concrete type (i.e, < String>) which is specified to be used right in place of the type parameter (i.e. < E>). There is the need to cast the retrieving elements from such things right as collections would surely be eliminated:
// Collection List/ArrayList with Generics List< String > iList = new ArrayList< String >(); iList.add(“Raj”); // Explicit cast not necessary String i = iList.get(0); // Collection List/ArrayList without Generics List iList = new ArrayList(); iList.add(“Raj”); // Explicit cast is necessary String i = (String)iList.get(0);
/∗∗ The java.util.List interface ∗/ public interface List { /∗∗ Returns the number of elements in this list. ∗/ int size(); /∗∗ Returns whether the list is empty. ∗/ boolean isEmpty(); /∗∗ Returns (but does not remove) the element at index i. ∗/ E get(int i) throws IndexOutOfBoundsException; /∗∗ Replaces the element at index i with e, and returns the replaced element. ∗/ E set(int i, E e) throws IndexOutOfBoundsException; /∗∗ Inserts element e to be at index i, shifting all subsequent elements later. ∗/ void add(int i, E e) throws IndexOutOfBoundsException; /∗∗ Removes/returns the element at index i, shifting subsequent elements earlier. ∗/ E remove(int i) throws IndexOutOfBoundsException; }
1. Create ArrayList,
2. Insert objects into ArrayList
3. Fetch objects on basis of index,
4. find a search for object in ArrayList,
5. insert a new item at specific index,
6. confirm state of ArrayList ( empty/fill),
7. using Iterator for ArrayList display,
8. find the size of the ArrayList
package listinterface;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
public class ListInterface { public static void main(String[] args) { String s; char ch; List myList; // create ArrayList myList=new ArrayList(); Scanner sc=new Scanner(System.in); while(true){ System.out.println("Enter String: "); s=sc.next(); myList.add(s); // insert objects into ArrayList System.out.println("Would you like to Continue: "); ch=sc.next().charAt(0); if(ch=='y') continue; else break; } System.out.println("Fetch objects on basis of index "); for(int c=0;c<myList.size();c++) // find size of the ArrayList System.out.println("Item: "+myList.get(c)); System.out.println("Search an item: "); // find a search for object in ArrayList String search=sc.next(); System.out.println("Index of "+search+" is "+myList.indexOf(search)); System.out.println("Enter new item: "); // insert a new item at specific index String new_intem=sc.next(); System.out.println("Enter position for new item: "); Integer pos=sc.nextInt(); myList.add(pos,new_intem); if(!myList.isEmpty()) // confirm state of ArrayList (empty/fill) { System.out.println("List not empty, so item are: "); for(int c=0;c<myList.size();c++) System.out.println("Item: "+myList.get(c)); } Iterator itr = myList.iterator(); // using Iterator for ArrayList display while(itr.hasNext()){ System.out.println(itr.next()); } } } Also Read, Tutorials Concept On Regular Expressions In Programming Tutorial On The Set Interface On Java
“I think the Software Testing Course is apt for me. Webskitters Academy has offered me the best experience for online training. Every learner can learn more about several concepts. The assignments to write Test cases and Test case Scenarios equipped me to learn to work on real-time projects. I got a job at a reputed firm with the help of the placement cell of this institute. I am extremely happy.”
“After the Software Testing course at Webskitters Academy, I now have the confidence to face testing interviews. I trusted the institute and here they gave me the best of the best. The assignments are great and they helped me to think out of the box and come up with new questions. I am extremely happy and satisfied and also got a job through this institute.”
Kolkata,India
“I am 100% content with the Software Testing course at Webskitters Academy. The professionals explain every question and doubt that aroused in my mind. The entire course is explained the thorough and step-by-step process. It is very professional but the learning method and environment are very friendly.”
Kolkata,India
“I am glad that I took the decision of joining Advanced PHP and MVC (Laravel) online course at Webskitters Academy. I joined the course during the lockdown, to learn programming. However, it is turned out to be more than just a course. It became my passion. The tutors were so good and encouraging. I even got a good placement during the pandemic, just a few days after the completion of the course.”
Kolkata,India
“Learnt PHP Laravel under Swarup Kumar Saha Sir. He is very helpful and excellent trainer. I am done this course online. The training was good I improved my coding skill and also improved my communication skills.”
Kolkata,India
“Learnt PHP With Laravel under Swarup Kumar Saha Sir. He is an excellent trainer. The training was good I improved my coding skills as well as communication skills.”
“I did training on Android App Development using Core Java from here. I guess, I couldn’t have find a better faculty than Swarup Sir. The thing I liked the most is that he is very friendly and always eager to help us on any terms. Apart from teaching us Android in a very great way, he helped us in personality development too, as he always motivated us in a good way. So, I guess these training days were not only for learning but also for finding our own skills and mastering them. Thanks to Webskitters for providing us this opportunity. I would like to learn more from here.”
Kolkata,India
“I had a fantastic experience with this academy. I trained by professionals. I learned Android App Development using Core Java properly and got a job very early in a reputed software company through this academy. Many many thanks to Amit sir, Riyanka ma’am and Debjit sir.”
Kolkata,India
“I have done Android App Development using Core Java training from this academy for one month. They cover most of topic in one month. Trainer (educator) was very supportive and polite.”
Kolkata,India
“Right after completing my Android App Development with KotlinCourse at Webskitters Academy, I landed a job in a reputed firm. This was an incredible experience for me as before that I was looking for a job for almost a year but failed to get any. Thanks to the professional training I took from the experts that helped me start my career instantly.”
Kolkata,India
We are glad to have hired the students from Webskitters Academy! We have to mention that they are trained to be the professionals. From the first day onwards, they have shown their excellence, and it is very impressive. Our team is also happy with their involvement and performance. Looking forward to hire more excellent students from them!
We are happy with the quality of training that the Webskitters Academy students have received. Few of them have been on-board with us recently and they have impressed us. We would recommend our associates to hire freshers from this institute, they make sure that the students are well-trained and prepared for the industry.
We hired the students from Webskitters Academy and to our surprise we found out that they are brilliant in their work! No way we can call them students, they are professionals. They know their work, have the skills and are well-groomed for the profession. Thanks, Webskitters Academy for such a comprehensive training!
“It was for the first time that we hired students from Websitters Academy. We had some doubts regarding freshers, but to our surprise, they are very good in their work. They know exactly how to do a particular job keeping the industry standards in mind. Our organization is happy to have them. Their codes, and development skills are perfect, suitable for the industry. It proves that they have been trained comprehensively. Good work by the faculties of Webskitters Academy. We will definitely recruit more young talents from you.”