Professional Documents
Culture Documents
Java Programs
Java Programs
html
http://phppot.com/php/mysql-blob-using-php/
https://www.youtube.com/watch?v=-fLtTRYQX0M&index=6&list=PLdFoBaBes4baoAa-
eTRJ1FVPR2CGglUsF
http://www.kean.edu/~ocisweb/downloads/FrontPage_2003/
WFDWY-XQXJF-RHRYG-BG7RQ-BBDHM
The ArrayList class extends AbstractList and implements the List interface.
ArrayList supports dynamic arrays that can grow as needed. In Java, standard arrays
are of a fixed length. After arrays are created, they cannot grow or shrink, which
means that you must know in advance how many elements an array will hold. But,
sometimes, you may not know until run time precisely how large of an array you
need. To handle this situation, the collections framework defines ArrayList. In
essence, an ArrayList is a variable-length array of object references. That is, an
ArrayList can dynamically increase or decrease in size. Array lists are created
with an initial size. When this size is exceeded, the collection is automatically
enlarged. When objects are removed, the array may be shrunk.
ArrayList( )
ArrayList(Collection c)
ArrayList(int capacity)
The first constructor builds an empty array list. The second constructor builds an
array list that is initialized with the elements of the collection c. The third
constructor builds an array list that has the specified initial capacity. The
capacity is the size of the underlying array that is used to store the elements.
The capacity grows automatically as elements are added to an array list.
The following program shows a simple use of ArrayList. An array list is created,
and then objects of type String are added to it. (Recall that a quoted string is
translated into a String object.) The list is then displayed. Some of the elements
are removed and the list is displayed again.
// Demonstrate ArrayList.
import java.util.*;
class ArrayListDemo {
public static void main(String args[]) {
// create an array list
ArrayList al = new ArrayList();
System.out.println("Initial size of al: " +
al.size());
// add elements to the array list
al.add("C");
al.add("A");
al.add("E");
al.add("B");
al.add("D");
al.add("F");
al.add(1, "A2");
System.out.println("Size of al after additions: " +
al.size());
// display the array list
System.out.println("Contents of al: " + al);
// Remove elements from the array list
al.remove("F");
al.remove(2);
System.out.println("Size of al after deletions: " +
al.size());
System.out.println("Contents of al: " + al);
}
}
The output from this program is shown here:
Here, cap is the new capacity. Conversely, if you want to reduce the size of the
array that underlies an ArrayList object so that it is precisely as large as the
number of items that it is currently holding, call
trimToSize( ), shown here:
void trimToSize( )
Obtaining an Array from an ArrayList
When working with ArrayList, you will sometimes want to obtain an actual array that
contains the contents of the list. As explained earlier, you can do this by calling
toArray( ). Several reasons exist why you might want to convert a collection into
an array such as:
// get array
Object ia[] = al.toArray();
int sum = 0;
// sum the array
for(int i=0; i<ia.length; i++)
sum += ((Integer) ia[i]).intValue();
System.out.println("Sum is: " + sum);
}
}
The output from the program is shown here:
Solution
This example displays the way of overloading a method depending on type and number
of parameters.
class MyClass {
int height;
MyClass() {
System.out.println("bricks");
height = 0;
}
MyClass(int i) {
System.out.println("Building new House that is " + i + " feet tall");
height = i;
}
void info() {
System.out.println("House is " + height + " feet tall");
}
void info(String s) {
System.out.println(s + ": House is " + height + " feet tall");
}
}
public class MainClass {
public static void main(String[] args) {
MyClass t = new MyClass(0);
t.info();
t.info("overloaded method");
//Overloaded constructor:
new MyClass();
}
}
Result
The above code sample will produce the following result.
110
40
==========
How to use method overloading for printing different types of array ?
Solution
This example displays the way of using overloaded method for printing types of
array (integer, double and character).
Solution
Following example compares two strings by using str compareTo (string), str
compareToIgnoreCase(String) and str compareTo(object string) of string class and
returns the ascii difference of first odd characters of compared strings.
System.out.println( str.compareTo(anotherString) );
System.out.println( str.compareToIgnoreCase(anotherString) );
System.out.println( str.compareTo(objStr.toString()));
}
}
true
false
String compare by == operator
public class StringCompareequl{
public static void main(String []args){
String s1 = "tutorialspoint";
String s2 = "tutorialspoint";
String s3 = new String ("Tutorials Point");
System.out.println(s1 == s2);
System.out.println(s2 == s3);
}
}
The above code sample will produce the following result.
true
false
==
How to split a string into a number of substrings ?
Solution
Following example splits a string into a number of substrings with the help of str
split(string) method and then prints the substrings.
feb
march
jan
jan
feb.march
feb.march
jan
feb.march
This is another example of string split
t
u
t
o
r
i
a
l
s
===========
Solution
This example shows you how to use codePointBefore() method to return the character
(Unicode code point) before the specified index.
=======
How to buffer strings ?
Solution
Following example buffers strings and flushes it by using emit() method.
Solution
Following example shows performance of concatenation by using "+" operator and
StringBuffer.append() method.
for(int i = 0;i<5000;i++) {
String result = "This is"
+ "testing the"
+ "difference"+ "between"
+ "String"+ "and"+ "StringBuffer";
}
long endTime = System.currentTimeMillis();
System.out.println("Time taken for string"
+ "concatenation using + operator : "
+ (endTime - startTime)+ " ms");
long startTime1 = System.currentTimeMillis();
for(int i = 0;i<5000;i++) {
StringBuffer result = new StringBuffer();
result.append("This is");
result.append("testing the");
result.append("difference");
result.append("between");
result.append("String");
result.append("and");
result.append("StringBuffer");
}
long endTime1 = System.currentTimeMillis();
System.out.println("Time taken for String concatenation"
+ "using StringBuffer : "
+ (endTime1 - startTime1)+ " ms");
}
}
=========
Solution
Following example shows how to use sort () and binarySearch () method to accomplish
the task. The user defined method printArray () is used to display the output:
import java.util.Arrays;
Linear Search
Following example shows search array element with Linear Search.
Bubble Sort
Following example shows sort array element with Bubble Sort.
Solution
Following example shows how to use sort () method and user defined method
insertElement ()to accomplish the task.
import java.util.Arrays;
Solution
Following example helps to determine the upper bound of a two dimensional array
with the use of arrayname.length.
Dimension 1: 2
Dimension 2: 5
Solution
This example shows how to search the minimum and maximum element in an array by
using Collection.max() and Collection.min() methods of Collection class .
import java.util.Arrays;
import java.util.Collections;
Solution
This example shows how to merge two arrays into a single array by the use of
list.Addall(array1.asList(array2) method of List class and Arrays.toString ()
method of Array class.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;