Save 2D List Data In 2D Array, Vice Versa

Sometimes we need an 2D Array in java. But we do not know the size. So we can not create it at declaration time. An flexible way to do this is first save the data in a java List, then translate the List data to it. In this article we will show you how to translate a List to Array and how to translate an Array back to List in java. Both or them maybe 1D or 2D(dimension).

1D List To 1D Array

	private String[] stringListToArray()
	{
		List<String> strList = new ArrayList<String>();
		strList.add("Java");
		strList.add("C++");
		strList.add("Python");
		strList.add("Visual Basic");
		strList.add("Objective C");
		strList.add("Java EE");
		
		String ret[] = {};
		ret = strList.toArray(ret);
		
		System.out.println("String in array : ");
		int len = ret.length;
		for(int i=0;i < len;i++)
		{
			String str = ret[i];
			System.out.println(str);
		}
		
		return ret;
	}

1D Array To 1D List

	private List<String> stringArrayToList()
	{
		String strArr[] = {"MySql", "Oracle" , "DB2", "PostgreSQL", "MongoDB", "MariaDB"};
		
		List<String> ret = new ArrayList<String>();
		int arrLen = strArr.length;
		for(int i=0;i<arrLen;i++)
		{
			String str = strArr[i];
			ret.add(str);
		}
		
		System.out.println("String in list : ");
		int size = ret.size();
		for(int i=0;i < size;i++)
		{
			String str = ret.get(i);
			System.out.println(str);
		}
		
		return ret;
	}

2D List To 2D Array

	private String[][] twoDimensionStringListToArray()
	{
		List<List<String>> dataList = new ArrayList<List<String>>();
		
		List<String> javaSkillList = new ArrayList<String>();
		javaSkillList.add("Jsp");
		javaSkillList.add("Servlet");
		javaSkillList.add("EJB");
		javaSkillList.add("JDBC");
		javaSkillList.add("JMS");
		javaSkillList.add("JVM");
		
		dataList.add(javaSkillList);
		
		List<String> javascriptSkillList = new ArrayList<String>();
		javascriptSkillList.add("JQuery");
		javascriptSkillList.add("ExtJs");
		javascriptSkillList.add("Dojo");
		javascriptSkillList.add("Jason");
		javascriptSkillList.add("Node.js");
		
		dataList.add(javascriptSkillList);
		
		
		String retArr[][] = null;
		if(dataList!=null)
		{
			 /* Get row count. */
			 int rowSize = dataList.size();
			 if(rowSize>0)
			 {
				 List<String> rowList = dataList.get(0);
				 
				 /* Get column count. */
				 int colSize = rowList.size();
				 
				 if(colSize>0)
				 {
					 /* Init the 2D array with the row and column count.*/
					 retArr = new String[rowSize][colSize];
					 
					 /* Loop for rows. */
					 for(int i=0;i<rowSize;i++)
					 {
						 /* Loop the columns for each row. */
						 rowList = dataList.get(i);
						 for(int j=0;j<rowList.size();j++)
						 {
							 /* Assign column data in one row.*/
							 retArr[i][j] = rowList.get(j);
						 }
					 }
				 }
			 }
		 }
		 return retArr;
	}

 

2D Array To 2D List

	private List<List<String>> twoDimensionStringArrayToList()
	{
		List<List<String>> retList = new ArrayList<List<String>>();
		
		String skillArr[][] = {{"Spring","Hibernate","Struts","Solr","Lucene"},{"Tomcat", "WebLogic", "WebSphere", "GlassFish", "JBoss"}};
	
		/* Get row count. */
		int len = skillArr.length;
		for(int i=0;i<len;i++)
		{
			String skill[] = skillArr[i];
			int skillLen = skill.length;
			
			List<String> skillList = new ArrayList<String>();
			for(int j=0;j<skillLen;j++)
			{
				skillList.add(skill[j]);
			}
			
			retList.add(skillList);
		}
		System.out.println(retList.toString());
		return retList;
	}

Download “List-Array-Translation-Example-Code.zip” List-Array-Translation-Example-Code.zip – Downloaded 44 times – 1 KB

(Visited 126 times, 1 visits today)
READ :   Java I/O(Input/Output) Overview

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.