<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <% List list = new ArrayList(); int cels = 3; //test1 String[] a = { "aaa", "1" };String[] b = { "bbb", "2" };String[] c = { "ccc", "1" };String[] d = { "ddd", "1" }; //test2 //String[] a = { "aaa", "2" };String[] b = { "bbb", "1" };String[] c = { "ccc", "1" };;String[] d = { "ddd", "2" }; //test3 //String[] a = { "aaa", "1" };String[] b = { "bbb", "2" };String[] c = { "ccc", "1" };;String[] d = { "ddd", "2" }; //test4 //String[] a = { "aaa", "1" };String[] b = { "bbb", "1" };String[] c = { "ccc", "2" };;String[] d = { "ddd", "2" }; //test5 //String[] a = { "aaa", "2" };String[] b = { "bbb", "2" };String[] c = { "ccc", "1" };;String[] d = { "ddd", "2" }; //test6 //String[] a = { "aaa", "2" };String[] b = { "bbb", "1" };String[] c = { "ccc", "2" };;String[] d = { "ddd", "2" }; //test7 //String[] a = { "aaa", "1" };String[] b = { "bbb", "2" };String[] c = { "ccc", "2" };;String[] d = { "ddd", "2" }; //test8 //String[] a = { "aaa", "2" };String[] b = { "bbb", "2" };String[] c = { "ccc", "2" };;String[] d = { "ddd", "2" }; list.add(a); list.add(b); list.add(c); list.add(d); String html = "<table border='1'>"; int size = list.size(); Integer strCel=0; for ( int i = 0; i < size; i++) { String[] strObj = (String[]) list.get(i); String str = strObj[0]; strCel = Integer.parseInt(strObj[1]); if (strCel == cels || strCel > cels) { html += (strCel!=1)?"<tr><td colspan='" + strCel + "'>" + str + "</td></tr>":"<tr><td>" + str + "</td></tr>"; } else { html += (strCel!=1)?"<tr><td colspan='" + strCel + "'>" + str + "</td>":"<tr><td>" + str + "</td>"; for (int j = i + 1; j < size; j++) { String[] strObj1 = (String[]) list.get(j); String str1 = strObj1[0]; Integer strCel1 = Integer.parseInt(strObj1[1]); strCel += strCel1; i++; if (strCel < cels) { html += (strCel1!=1)?"<td colspan='" + strCel1 + "'>" + str1 + "</td>":"<td>" + str1 + "</td>"; } else if (strCel > cels) { strCel -= strCel1; i--; int cel2=cels-strCel; for(int k=0;k<cel2;k++) html += "<td> </td>"; break; } else { html += (strCel1!=1)?"<td colspan='" + strCel1 + "'>" + str1 + "</td></tr>":"<td>" + str1 + "</td></tr>"; break; } } if(i+1>=size){ int cel2=cels-strCel; for(int k=0;k<cel2;k++) html += "<td> </td>"; html+="</tr>"; } } } html+="</table>"; System.out.println(html); out.println(html); %> </body> </html>