如何在 JList 中组合 2 个向量?

发布于 2024-10-18 20:03:48 字数 1268 浏览 0 评论 0原文

Vector<String> totalProducts = Products.getProductNames();
Vector<String> selectedProducts = Products.getSelectedProductNames();

selectedProducts 向量是 totalProducts 的子向量(这意味着 selectedProducts 包含 totalProducts 中的一个、多个或全部元素>)。我想要的是将这两个向量组合起来并生成一个JList,其中包含totalProducts 中的所有元素,以及已选择 的selectedProducts 元素。

我尝试过的:

    Vector<Integer> indices = new Vector<Integer>();
    JList prdList = new JList(totalProducts);
    for(int i = 0; i < totalProducts.size(); i++)
    {
        for(String name : selectedProducts)
        {
            if(totalProducts.contains(name)) indices.add(i);
        }
    }
    Object [] objIndices = indices.toArray();
    //... Cast from Object [] to int [] ....
    prdList.setSelectedIndices(intIndices);

...但这会选择最终 JList 中的所有元素。

以前我尝试过:

    JList prdList = new JList(totalProducts);
    for(String tName : totalProducts)
    {
        for(String sName : selectedProducts)
        {
            if(totalProducts.contains(sName)) prdList.setSelectedValue(sName, false);
        }
    }

...但是这个仅选择了 selectedProducts 中的最后一个元素。

你能帮我做对吗?

Vector<String> totalProducts = Products.getProductNames();
Vector<String> selectedProducts = Products.getSelectedProductNames();

The selectedProducts vector is a subvector of totalProducts (meaning that selectedProducts contains one, more or all of the elements from totalProducts). What I want is to combine these two vectors and make a single JList, which contains all the elements from totalProducts, and with the elements of selectedProducts already selected.

What I tried:

    Vector<Integer> indices = new Vector<Integer>();
    JList prdList = new JList(totalProducts);
    for(int i = 0; i < totalProducts.size(); i++)
    {
        for(String name : selectedProducts)
        {
            if(totalProducts.contains(name)) indices.add(i);
        }
    }
    Object [] objIndices = indices.toArray();
    //... Cast from Object [] to int [] ....
    prdList.setSelectedIndices(intIndices);

...but this selects all the elements in the final JList.

Previously I tried:

    JList prdList = new JList(totalProducts);
    for(String tName : totalProducts)
    {
        for(String sName : selectedProducts)
        {
            if(totalProducts.contains(sName)) prdList.setSelectedValue(sName, false);
        }
    }

...but this one selected only the last element from the selectedProducts.

Can you please help me to do it right?

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

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

发布评论

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

评论(2

我不咬妳我踢妳 2024-10-25 20:03:48

您之所以尝试选择所有项目,是因为您正在迭代每个项目,并且如果 selectedProducts 列表中的任何项目位于总列表中,则会将迭代项目的索引添加到最终选择列表中。尝试将循环更改为如下所示:

for(int i = 0; i < totalProducts.size(); i++)
{
    String name = totalProducts.get(i);
    if(selectedProducts.contains(name)) indices.add(i);
}

Your attempt that selects all items does so because you're iterating over each item, and if any item from the selectedProducts list is in the total list, adds the iteration item's index to the final selection list. Try changing your loop to something like this:

for(int i = 0; i < totalProducts.size(); i++)
{
    String name = totalProducts.get(i);
    if(selectedProducts.contains(name)) indices.add(i);
}
别靠近我心 2024-10-25 20:03:48

在调试你的第一次尝试时(看起来应该工作,你的intIndices数组的内容是什么?因为假设你的数组转换工作,它看起来应该工作。

但是,因为selectedproducts 保证项目数少于总数,

List<Integer> indices = new ArrayList<Integer>(selectedProducts.size());    
for(String name : selectedProducts)
{
    int index = totalProducts.indexOf(name);
    if (index != -1)
        indices.add(index);        
}

但您可能想对其进行迭代?因为 indexOf 是通过列表进行线性搜索,因此它可能不会产生太大影响。无论哪种方式都有所不同,

对于您的第二次尝试,ListSelectionModel 具有添加选定索引的方法(addSelectionInterval(int index0, int index1)
,您正在使用设置(覆盖)选择的那个。

请参阅http://download.oracle.com/javase/ 6/docs/api/javax/swing/ListSelectionModel.html

:您可能想使用 List<> 而不是 Vector<>,如下所示Vector 有很多不必要的同步开销。除非您需要同步....

编辑修复了add(i)与add(index)的复制+粘贴

in debugging your first attempt (which looks like it should work, what was the contents of your intIndices array? because that looks like it should work, presuming your array conversion works.

however, since selectedproducts is guaranteed to be less items than total, you might want to iterate over that instead?

List<Integer> indices = new ArrayList<Integer>(selectedProducts.size());    
for(String name : selectedProducts)
{
    int index = totalProducts.indexOf(name);
    if (index != -1)
        indices.add(index);        
}

although, since indexOf is a linear search through a list, it probably doesn't make much of a difference either way.

as for your second attempt, the ListSelectionModel has methods for adding a selected index (addSelectionInterval(int index0, int index1))
, you're using the one that sets (overwrites) the selection.

see http://download.oracle.com/javase/6/docs/api/javax/swing/ListSelectionModel.html

aside: you might want to use List<> instead of Vector<>, as vector has a lot of unecessary synchronization overhead. Unless you need the synchronization....

edit fixed copy+paste of add(i) with add(index)

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