通过 JTextField 的内容搜索 arraylist - arraylist.contains(jtextfield)

发布于 2024-12-19 09:13:40 字数 781 浏览 1 评论 0原文

目前,我正在尝试编写一个系统,重点是将数据输入到列表和数组列表中,并实现搜索和排序功能。

目前在我的系统上,我有:

一个用于存储数据的类 一个 arraylist 类,它从数据存储类中的对象获取数据。 最后是一个 swing GUI 类,其中包含一个显示 Arraylist 的 JList。

我想做的是使用 JButton 动作侦听器搜索数组列表,然后将搜索结果输出到 JList。

JButton 将获取 JTextField 的内容,并检查该字符串是否存在于 ArrayList 中。

我的问题首先是,我将如何在 arraylist 类中创建搜索函数,并在单独的类中调用 JTextField 的内容?

其次,我是否需要将 jtextfield 转换为字符串,然后才能在数组列表上调用 .contains 方法?

第三,一旦实现了搜索功能,如果搜索的文本存在,我将如何从数组列表中选择记录

这是我的数据存储类: http://pastebin.com/hwyD8r1j

我的 arraylist 类: http://pastebin.com/d3ftLsJb

我不希望你们为我写它,尽管那会乖一点,哈哈

但是任何有关我如何在数组列表中实现此功能的指示或见解将不胜感激,

哦,如果您需要我发布我的 GUI 类,请询问。

Currently, I'm trying to write a system, which is focused around inputting data into lists and arraylists, and implementing search and sort functionality.

Currently on my system, I have:

A class to store data
An arraylist class which gets data from an object within the data storage class.
And finally, a swing GUI class, which contains a JList which displays the Arraylist.

What i'm trying to do is search through the arraylist with a JButton actionlistener, and then output the results on the search to the JList.

The JButton would take the contents of a JTextField, and check if the string is present in the ArrayList.

My question first of all is, how would I go about creating a search function in the arraylist class, and call the contents of a JTextField in a seperate class?

Secondly, would I need to convert the jtextfield to a string before I could call a .contains method on the arraylist?

and thirdly, once the search function is implemented, how would I go about selecting a record from the arraylist if the text searched for is present

Here is my Data storage class:
http://pastebin.com/hwyD8r1j

My arraylist class:
http://pastebin.com/d3ftLsJb

I'm not expecting you guys to write it for me, although that would be nice, haha.

But any pointers or insight on how I could go about implementing this functionality into my arraylist would be appreciated,

Oh and if you need me to post my GUI class, just ask.

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

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

发布评论

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

评论(1

难如初 2024-12-26 09:13:40

在 JTextField 上调用“getText”以获取他们输入的字符串。你基本上会做类似下面的事情。

// Somehow you've initialized your array list
List<String> data = ...;

// within your action listener - invoked when the button is clicked.  You'll need to
// make sure the textField is "final"
String selected = textfield.getText();
// Linear search through your strings for one matching your text
for (String datum : data) {
  if (selected.contains(datum)) {
    // do whatever you want here; you found a match
  }
}

Call 'getText' on the JTextField to get the string that they've entered. You'll basically do something like the following.

// Somehow you've initialized your array list
List<String> data = ...;

// within your action listener - invoked when the button is clicked.  You'll need to
// make sure the textField is "final"
String selected = textfield.getText();
// Linear search through your strings for one matching your text
for (String datum : data) {
  if (selected.contains(datum)) {
    // do whatever you want here; you found a match
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文