数组未显示在 Jlist 中但已填充到控制台

发布于 2024-08-30 05:15:40 字数 2872 浏览 1 评论 0原文

嘿。今天是一个忙碌的调试者。 我会给出这个简短的版本。

我制作了一个从数据库中获取名称的数组列表。 然后我将 arraylist 的内容放入字符串数组中。 现在我也想在 JList 中显示数组内容。

奇怪的是它之前就可以工作。我有两种方法。也只需一点练习即可确保我正确添加到 Jlist 中。这是关键代码。

这是我的代码的布局。

变量 构造函数 方法

我的变量中的

String[] contactListNames = new String[5];
ArrayList<String> rowNames = new ArrayList<String>();
JList contactList = new JList(contactListNames);

我已经定义了这 3 个足够简单的方法。

在我的构造函数中我再次拥有它们。

contactListNames = new String[5];
    contactList = new JList(contactListNames);
//i dont have the array list defined though.

 printSqlDetails();
// the prinSqldetails was too make sure that the connectionw as alright. and its working fine.

     fillContactList();
 // this is the one thats causing me grief. its where all the work happens.

   // fillContactListTest();
// this was the tester that makes sure its adding to the list alright.

这是 fillContactListTest() 的代码,

  public void fillContactListTest()
 {
  for(int i = 0;i<3;i++)
  {
   try
   {
    String contact;
    System.out.println(" please fill the list at index "+ i);
    Scanner in = new Scanner(System.in);
    contact = in.next();
    contactListNames[i] = contact;
    in.nextLine();
   }
   catch(Exception e)
   {
 e.printStackTrace();   
   }
  }
 }

这是主要的代码,应该也可以工作。

public void fillContactList()
       {
           int i =0;
           createConnection();
           ArrayList<String> rowNames = new ArrayList<String>();
           try
           {
               Statement stmt = conn.createStatement();
               ResultSet namesList = stmt.executeQuery("SELECT name FROM Users");
               try
               {

                   while (namesList.next())
                   {
                       rowNames.add(namesList.getString(1));

                       contactListNames =(String[])rowNames.toArray(new String[rowNames.size()]);

                       // this used to print out contents of array list
                      // System.out.println("" + rowNames);

                       while(i<contactListNames.length)
                       {
                           System.out.println(" " + contactListNames[i]);
                           i++;
                       }
                   }
               }
               catch(SQLException q)
               {
                   q.printStackTrace();
               }
               conn.commit();
               stmt.close();
               conn.close();
           }
           catch(SQLException e)
           {
               e.printStackTrace();
           }
       }

我真的需要帮助。我束手无策。我只是不明白为什么第一个方法添加到 JList 没有问题。但第二个不会。

contactListNames 数组和数组列表都可以正常打印并包含名称。但我一定也把它们转移到了错误的 jlist 中。请帮助

p.s 我知道这很长。但相信我,这是简短的版本。

Hey there. been a busy debugger today.
ill give ths short version.

ive made an array list that takes names from a database.
then i put the contents of the arraylist into an array of strings.
now i want too display the arrays contents in a JList.

the weird thing is it was working earlier. and ive two methods. ones just a little practice too make sure i was adding to the Jlist correctly. so heres the key codes.

this is the layout of my code.

variables
constructor
methods

in my variables i have these 3 defined

String[] contactListNames = new String[5];
ArrayList<String> rowNames = new ArrayList<String>();
JList contactList = new JList(contactListNames);

simple enough.

in my constructor i have them again.

contactListNames = new String[5];
    contactList = new JList(contactListNames);
//i dont have the array list defined though.

 printSqlDetails();
// the prinSqldetails was too make sure that the connectionw as alright. and its working fine.

     fillContactList();
 // this is the one thats causing me grief. its where all the work happens.

   // fillContactListTest();
// this was the tester that makes sure its adding to the list alright.

heres the code for fillContactListTest()

  public void fillContactListTest()
 {
  for(int i = 0;i<3;i++)
  {
   try
   {
    String contact;
    System.out.println(" please fill the list at index "+ i);
    Scanner in = new Scanner(System.in);
    contact = in.next();
    contactListNames[i] = contact;
    in.nextLine();
   }
   catch(Exception e)
   {
 e.printStackTrace();   
   }
  }
 }

heres the main one thats supposed too work.

public void fillContactList()
       {
           int i =0;
           createConnection();
           ArrayList<String> rowNames = new ArrayList<String>();
           try
           {
               Statement stmt = conn.createStatement();
               ResultSet namesList = stmt.executeQuery("SELECT name FROM Users");
               try
               {

                   while (namesList.next())
                   {
                       rowNames.add(namesList.getString(1));

                       contactListNames =(String[])rowNames.toArray(new String[rowNames.size()]);

                       // this used to print out contents of array list
                      // System.out.println("" + rowNames);

                       while(i<contactListNames.length)
                       {
                           System.out.println(" " + contactListNames[i]);
                           i++;
                       }
                   }
               }
               catch(SQLException q)
               {
                   q.printStackTrace();
               }
               conn.commit();
               stmt.close();
               conn.close();
           }
           catch(SQLException e)
           {
               e.printStackTrace();
           }
       }

i really need help here. im at my wits end. i just cant see why the first method would add to the JList no problem. but the second one wont.

both the contactListNames array and array list can print fine and have the names in them. but i must be transfering them too the jlist wrong. please help

p.s im aware this is long. but trust me its the short version.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晚雾 2024-09-06 05:15:40

您需要

contactList = new JList(contactListNames);

之后

fillContactList();

调用,因为在fillContactList()内,您替换的是contactListNames引用而不是 >填充它。

也就是说并且与所描述的问题无关,代码中存在更多可能性,但我认为您迟早会发现它们:) (代码泄漏 JDBC 连接、代码流程效率低下、代码未以直观的 OO 方式排列等等。但是,这与所描述的问题完全无关)。

You need to call

contactList = new JList(contactListNames);

after

fillContactList();

Because inside the fillContactList() you're replacing the contactListNames reference instead of filling it.

That said and unrelated to the described problem, there are more odds in the code, but you'll find them out sooner or later I think :) (the code is leaking JDBC connections, the code flow is inefficient, the code is not arranged in an intuitive OO way, etc.. but, again, this has all nothing to do with the described problem).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文