Showing posts with label JAVA. Show all posts
Showing posts with label JAVA. Show all posts

Convert String Array to Arraylist

I was working on collections in one of my project and while working on copying arrays to collections found below two approaches to do it. These two approaches have their advantages as well as limitations.



Solution 1:
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;

public class StringArrayToCollectionSample
{
   public static void main(String[] args)
   {
      String[] strArray = new String[] {"Hello", "World", "Application"};
      List<String> strList = Arrays.asList(strArray);


   }
}

This approach will work fine if you only need read access to the array as if it is a List and you don't want to add or remove elements from the list. As it doesn't need to copy the content of the array. This method returns a List that is a "view" onto the array or you can say a wrapper that makes the array look like a list.Most important is that the list is of fixed size, i.e if you try to add elements to the list, you'll get an java.lang.UnsupportedOperationException.

Solution 2:
public class StringArrayToCollectionSample
{
   public static void main(String[] args)
   {
      String[] strArray = new String[] {"Hello", "World", "Application"};
      List<String> strList = new ArrayList(Arrays.asList(strArray));

   }
}

This approach will copies the content of the array to a new ArrayList. Advantage of this approach is that you can easily add or remove element from the List.

 

 

GRAILS - Installation and Configuration

Recently I got a chance to work on a project based on GRAILS. At first I was tense and even I have an argument with my Project lead, that why he is giving me a new challenge? Instead of listening to me he just told me learn the framework and build solution required by client as per requirement.

So at last I started goggling with the GRAILS, like searching for tutorials, how to configure it and on top How I am going to create application using Eclipse. At last when I had enough of the taste, I just thought of making a bunch of tutorials and keep it one place. Given below is the first part of tutorial covering the installation and configuring GRAILS at your own machine.

1. Configure Java

Before moving to installation of GRAILS ensure that you have a  Java installed, If not download it from http://www.oracle.com/technetwork/java/javase/downloads/index.html and install it. After then configure the JAVA_HOME environment variable at your jdk installation directory. For Example if you installed jdk in C:\Java6\jdk1.6.0_10 then your JAVA_HOME variable will point to C:\Java6\jdk1.6.0_10. After then modify the environment variable PATH, In windows append %JAVA_HOME%\bin with a semi-colon(;) to it. Once it is done check by typing java -version at command prompt, If it is configured correctly It will be displaying the version you have installed.

2. Configure GRAILS

After installation and configuring JAVA, download the latest version of GRAILS from http://grails.org/Download. Unzipped the GRAILS zipped download in a new directory. Make sure none of the directory have blank space in them. After then create a new environment variable GRAILS_HOME and point it to your GRAILS directory. Example If you have unzipped GRAILS at C:\grails, then GRAILS_HOME will have the value C:\grails. Also modify the PATH variable and append %GRAILS_HOME%\bin to it(Windows).
Once it is done type grails at command prompt to verify that Grails is installed and ready to use. If successfully installed and configured it will display welcome message from GRAILS and will display the version installed.





Struts 2 - HTML Snippets Problem

To display the value of a field present in form, you can use struts2 <s:property> tag. For example :

<s:property value="address"></s:property>

But to display the HTML snippet in field value you have to use the escape flag, Like :

<s:property escape="false" value="address"></s:property>

More stories on struts2



struts2 - Displaying Checkboxlist Vertically
File Download Issue in Internet Explorer - struts2
struts2 Interview questions - I