Java J2ME JSP J2EE Servlet Android

Java Vector to array or List to array

We can easily get an array from Vector or List by just calling toArray() methods. This will generate arrays of Objects. Here is an example of getting array of Strings from java.util.Vector and java.util.List. I hope this will be helpful to you..

Vector friends = new Vector();
friends.add("Tuhin");
friends.add("Sumon");
friends.add("Habib");
friends.add("Shishir");
friends.add("Kiser");
friends.add("Zimi");
friends.add("Lizzie");

String arr[] = (String[]) friends.toArray(new String[0]);
System.out.println(Arrays.toString(arr));

List friendsList = new ArrayList();
friendsList.add("Tuhin");
friendsList.add("Sumon");
friendsList.add("Habib");
friendsList.add("Shishir");
friendsList.add("Kiser");
friendsList.add("Zimi");
friendsList.add("Lizzie");
arr = (String[]) friendsList.toArray(new String[0]);
System.out.println(Arrays.toString(arr));

If you execute the above code block. This will generate an output as follows..

[Tuhin, Sumon, Habib, Shishir, Kiser, Zimi, Lizzie]
[Tuhin, Sumon, Habib, Shishir, Kiser, Zimi, Lizzie]

Java Sorting Array, List

Java provides an interface java.util.Collections which provides way to sort List, Arraylist etc. We can sort an array of String by first transforming it to a List first then applying Collection.sort() method then again transforming from List to String array. Here is a practical example of this. Hope this will be helpful to you.

String friends[] = {"Tuhin", "Sumon","Habib","Shishir","Kiser","Zimi","Lizzie"};
System.out.println(Arrays.toString(friends));
List tempList = Arrays.asList(friends);
System.out.println(tempList);
Collections.sort(tempList);
System.out.println(tempList);
friends = (String[]) tempList.toArray(new String[0]);
System.out.println(Arrays.toString(friends));

This code block if execute will generate output like ...

[Tuhin, Sumon, Habib, Shishir, Kiser, Zimi, Lizzie]
[Tuhin, Sumon, Habib, Shishir, Kiser, Zimi, Lizzie]
[Habib, Kiser, Lizzie, Shishir, Sumon, Tuhin, Zimi]
[Habib, Kiser, Lizzie, Shishir, Sumon, Tuhin, Zimi]

ORACLE: Getting maximum sub string

We can easily find the maximum sub-string from a table of oracle. Let's see the following example..

CREATE table test(
name varchar2(100),
code varchar2(50)
)
INSERT INTO test(name,code) VALUES('Bangladesh','880');
INSERT INTO test(name,code) VALUES('BGD-Mobile','8801');
INSERT INTO test(name,code) VALUES('BGD-GP','88017');
INSERT INTO test(name,code) VALUES('BGD-AKTEL','88018');
INSERT INTO test(name,code) VALUES('BGD-Bangla Link','88019');
INSERT INTO test(name,code) VALUES('BGD-CITYCELL','88011');
INSERT INTO test(name,code) VALUES('BGD-WARID','88016');
INSERT INTO test(name,code) VALUES('BGD-TELETALK','88015');

SELECT * FROM test

SELECT max(code) FROM test WHERE instr('8801234',code)>0;

This will give: 8801


SELECT max(code) FROM test WHERE instr('880234',code)>0;

This will give: 880


SELECT max(code) FROM test WHERE instr('8801123',code)>0;

This will give: 88011

ORA-01704: String literal too long

The Mail cause of this error is stating by the message itself 'String literal too long'. If you try to INSERT more than 4000 characters to a varchar2 field this error will occur. Another case where this may occur is if you try to update a CLOB field. CLOB field doesn't support update.

In Java if you try to execute PreparedStatement.executeUpdate() to INSERT or UPDATE CLOB field of ORACLE with more than 4000 characters, you'll get this error.

To avoid this use PreparedStatement.execute() methode to to INSERT CLOB field.

This may occur in other language also. And you can use same technique to avoid errors.

Happy programming.

Comment my blog/ email me for any problems related to this.

Java by Example: Exception Handling, creating custom exception class

All related Posts
Java by Example: Exception Handling, creating custom exception class
Java by Example: Exception handling – throw custom Exception
Java By Example: Exception Handling – Throwing exception from method
Java by Example: Exception handling – Basic Exception Handling example ‘try catch’
Java by Example: Exception Handling – Java Exception Basics


You can easily create your own Exception Class where you can put your own exception. To do this what you have to do is just you have create an subclass of java.lang.Exception class.

Here is a basic example of creating custom exception class

package example.java.lang;

public
class
ExceptionExp5
extends Exception{

    public ExceptionExp5() {

        super("[ MY EXCEPTION ] ");

    }

    public ExceptionExp5(String exceptionString) {

        super("[ MY EXCEPTION ] "+exceptionString);

    }

    

    public
static
void main(String args[]){

        try{

            new
ExceptionExp5().tryCustomException();

        }catch(Exception e){

            e.printStackTrace();

        }

    }

    public
void tryCustomException() throws Exception{

        int fibs[]={1,2,3,5,8,10,13};

        for(int i=0;i<fibs.length;i++){

            if(isFibs(fibs[i]))

                System.out.println(fibs[i]+" - OK");

            else

                throw
new
ExceptionExp5(
"Invalid fibonacci number "+fibs[i]);

        }

    }

    private
boolean isFibs(int num){

        int f0 = 1;

        int f1 = 1;

        int fib = 1;

        while(num>=fib){

            if(num==fib)

                return
true;

            fib = f0 + f1;

            f0 = f1;

            f1 = fib;

        }

        return
false;

    }

}

If you execute this you'll get output like..

1 - OK

2 - OK

3 - OK

5 - OK

8 - OK

example.java.lang.ExceptionExp5: [ MY EXCEPTION ] Invalid fibonacci number 10

    at example.java.lang.ExceptionExp5.tryCustomException(ExceptionExp5.java:23)

    at example.java.lang.ExceptionExp5.main(ExceptionExp5.java:12)