使用java在文本区域中并排显示字符串数组中的对象
我想读写在文本文件中的特定列,并将这些特定列并排显示在文本区域中。我设法读取所需的列,并使用以下代码将它们显示到我的文本区域:使用java在文本区域中并排显示字符串数组中的对象,java,arraylist,Java,Arraylist,我想读写在文本文件中的特定列,并将这些特定列并排显示在文本区域中。我设法读取所需的列,并使用以下代码将它们显示到我的文本区域: try { ArrayList<String> totalResult1 = new ArrayList<String>(); ArrayList<String> totalResult2 = new ArrayList<String>(); [enter image
try
{
ArrayList<String> totalResult1 = new ArrayList<String>();
ArrayList<String> totalResult2 = new ArrayList<String>();
[enter image description here][1]ArrayList<String> totalResult3 = new ArrayList<String>();
try
{
FileInputStream fStream = new FileInputStream("hubo\\" + "table" + ".txt");
DataInputStream in = new DataInputStream(fStream);
BufferedReader br = new BufferedReader (new InputStreamReader(in));
String strLine;
while((strLine = br.readLine()) != null)
{
strLine = strLine.trim();
if((strLine.length()!=0) && (strLine.charAt(0) !='#'))
{
String[] employee = strLine.split("\\s+");
totalResult1.add(employee[0]);
totalResult2.add(employee[1]);
totalResult3.add(employee[2]);
}
}
for(String s1 : totalResult1)
{
showArea.append(s1.toString() + "\n");
}
for(String s2 : totalResult2)
{
showArea.append("\t" + "\t" + s2.toString() + "\n");
}
in.close();
}
catch (Exception e1)
{
}
}
catch(Exception e1)
{
}
我期望的结果是:
Alex Santos Married
Troy Smith Single
John Love Married
我想将我的两列并排显示到我的文本区域,有人能给我指出正确的方向吗。您的解决方案很接近,但不完全正确。当您从
totalResult1
中添加员工姓名时,每次都会转到新行。因此,当您添加第二个列表中的值时,您已经在使用名称了。要创建类似表格的显示,您需要同时从每个列表中添加值:
for(int i = 0; i < totalResult1.size(); i++){
showArea.append(totalResult1.get(i) + "\t\t");
showArea.append(totalResult2.get(i) + "\n");
}
for(int i=0;i
他们应该做到这一点。但一般来说,当您想要一个表时,不应该使用文本区域,您可以使用表控件 谢谢你的帮助。我已经解决了这个问题。谢谢你的解决方案
for(int i = 0; i < totalResult1.size(); i++){
showArea.append(totalResult1.get(i) + "\t\t");
showArea.append(totalResult2.get(i) + "\n");
}