Blog Archive

Monday, April 10, 2017

Using arrayList & io as workbench for your work around

Om

simplify your work with java arraylist.


example: 
some rows in file

Scan all those (each row as entry in array list)
sort, check with another string, find index of any row


importance arralyList indexOf and String indexOf                  compare

ArrayList- -------    contains

Collections -------------;sort

String --------------------Trim

( all are explained with example) 

Example :
In this example, file1.txt ( certain entries)
TestReplace.java is java code reading this text file and placing all rows in arrayList.

Now, we can do various operations on this.
1. reading specific row
2. Update specific row
3.search with "rowcontent" if is in arrayList or not.
4.Sort the arrayList
5.Find the row number or Index Number of "row content"
6. comparision of String IndexOf and ArrayList indexOf
7.Arraylist contains









--------------------------------------------------------------------------------------------------------------
Another example : arrayList usage

Other userful operation with array list

 ArrayList obj = new ArrayList();

 /*This is how elements should be added to the array list*/
 obj.add("Ajeet");
 obj.add("Harry");
 obj.add("Chaitanya");
 obj.add("Steve");
 obj.add("Anuj");

 /* Displaying array list elements */
 System.out.println("Currently the array list has following elements:"+obj);

 /*Add element at the given index*/
 obj.add(0, "Rahul");
 obj.add(1, "Justin");

 /*Remove elements from array list like this*/
 obj.remove("Chaitanya");
 obj.remove("Harry");

 System.out.println("Current array list is:"+obj);

 /*Remove element from the given index*/
 obj.remove(1);


String array to ArrayList







ArrayList methods for use:

1) add( Object o): This method adds an object o to the arraylist.

obj.add("hello");
This statement would add a string hello in the arraylist at last position.

2) add(int index, Object o): It adds the object o to the array list at the given index.

obj.add(2, "bye");
It will add the string bye to the 2nd index (3rd position as the array list starts with index 0) of array list.

3) remove(Object o): Removes the object o from the ArrayList.

obj.remove("Chaitanya");
This statement will remove the string “Chaitanya” from the ArrayList.

4) remove(int index): Removes element from a given index.

obj.remove(3);
It would remove the element of index 3 (4th element of the list – List starts with o).

5) set(int index, Object o): Used for updating an element. It replaces the element present at the specified index with the object o.

obj.set(2, "Tom");
It would replace the 3rd element (index =2 is 3rd element) with the value Tom.

6) int indexOf(Object o): Gives the index of the object o. If the element is not found in the list then this method returns the value -1.

int pos = obj.indexOf("Tom");
This would give the index (position) of the string Tom in the list.

7) Object get(int index): It returns the object of list which is present at the specified index.

String str= obj.get(2);
Function get would return the string stored at 3rd position (index 2) and would be assigned to the string “str”. We have stored the returned value in string variable because in our example we have defined the ArrayList is of String type. If you are having integer array list then the returned value should be stored in an integer variable.

8) int size(): It gives the size of the ArrayList – Number of elements of the list.

int numberofitems = obj.size();
9) boolean contains(Object o): It checks whether the given object o is present in the array list if its there then it returns true else it returns false.

obj.contains("Steve");
It would return true if the string “Steve” is present in the list else we would get false.

10) clear(): It is used for removing all the elements of the array list in one go. The below code will remove all the elements of ArrayList whose object is obj.

obj.clear();

------------------------------------------------------------------------------------------
Array List methods;
T[] toArray(T[] a):Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array.
boolean add(E e):Appends the specified element to the end of this list.
boolean addAll(Collection c):Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator.
boolean addAll(int index, Collection c):Inserts all of the elements in the specified collection into this list, starting at the specified position.
boolean contains(Object o):Returns true if this list contains the specified element.
boolean isEmpty():Returns true if this list contains no elements.
boolean remove(Object o):Removes the first occurrence of the specified element from this list, if it is present.
boolean removeAll(Collection c):Removes from this list all of its elements that are contained in the specified collection.
boolean retainAll(Collection c):Retains only the elements in this list that are contained in the specified collection.
E get(int index):Returns the element at the specified position in this list.
E remove(int index):Removes the element at the specified position in this list.
E set(int index, E element):Replaces the element at the specified position in this list with the specified element.
int indexOf(Object o):Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
int lastIndexOf(Object o):Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.
int size():Returns the number of elements in this list.
Iterator iterator():Returns an iterator over the elements in this list in proper sequence.
List subList(int fromIndex, int toIndex):Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.
ListIterator listIterator():Returns a list iterator over the elements in this list (in proper sequence).
ListIterator listIterator(int index):Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.
Object clone():Returns a shallow copy of this ArrayList instance.
Object[] toArray():Returns an array containing all of the elements in this list in proper sequence (from first to last element).
protected void removeRange(int fromIndex, int toIndex):Removes from this list all of the elements whose index is between fromIndex, inclusive, and toIndex, exclusive.
void add(int index, E element):Inserts the specified element at the specified position in this list.
void clear():Removes all of the elements from this list.
void ensureCapacity(int minCapacity):Increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.
void trimToSize():Trims the capacity of this ArrayList instance to be the list's current size.

String JAVA API : standard methods from API

Which returns as Integer:


Which returns boolean , used for conditino validation





String array;







Below example, helps how to put condition while reading file reader, placing in arraylist( whatever we required).
later we can print arrayList as we required.


No comments:

Post a Comment