在ArrayList和检索的自定义对象 [英] Custom Object in ArrayList and Searchable

查看:160
本文介绍了在ArrayList和检索的自定义对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个称为文件类型的自定义对象

I have created a custom object called FileType

import javax.swing.ImageIcon;

class FileType
{
    private int     index;
    private String  type;
    private String  extension;
    private String  description;
    ImageIcon       icon;

    public FileType(int index, String type, String extension, String description, String icon)
    {
        this.index = index;
        this.type = type;
        this.extension = extension;
        this.description = description;
        this.icon = Utils.createImageIcon(icon);
    }

    public int getIndex()
    {
        return index;
    }

    public String getType()
    {
        return type;
    }

    public String getExtension()
    {
        return extension;
    }

    public String getDescription()
    {
        return description;
    }

    public ImageIcon getIcon()
    {
        return icon;
    }
}

编辑:我还创建了一个文件列表类

I also created a FileList class

import java.util.AbstractList;
import java.util.ArrayList;

public class FileList extends AbstractList
{

    private ArrayList<FileType> fileList;

    public FileList()
    {
    }

    public void add(int index, String type, String extension, String description, String icon)
    {
        FileType data = new FileType(index, type, extension, description, icon);
        if (!fileList.contains(data))
        {
            fileList.add(data);
        }
    }

    @Override
    public Object get(int index)
    {
        return fileList.toArray()[index];
    }

    @Override
    public int size()
    {
        return fileList.size();
    }
}

现在我想创建对象,如


  • 1,HTML,ASCII HTML文件,图像/ html.png

  • 2,HTML,引导HTML文件,图像/ html.png

现在我失去了,因为我想说这样说:

Now I am lost because i want to say say something like:

list.findelementbytype(HTML);这将返回文件类型对象,然后我可以访问和读取其余值/属性。

list.findelementbytype ("html"); which would return FileType object, which i could then access and fetch the remaining values/attributes.

我在正确的轨道上还是我做这错了吗?这是一个文件选择我写,我想拥有的所有相关数据的一个对象。更OO,谢谢!

Am i on the right track or am i doing this wrong? This is for a File Chooser i am writing and i wanted to have all of the relevant data in a single object. More OO, thank you!

SOLUTION

它创建的每个数据条目的文件类型的类。

The File Type Class which creates each data entry.

import javax.swing.ImageIcon;

class FileType
{
    private int     index;
    private String  search;
    private String  type;
    private String  extension;
    private String  description;
    ImageIcon       icon;

    public FileType(int index, String search, String type, String extension, String description, String icon)
    {
        this.index = index;
        this.search = search;
        this.type = type;
        this.extension = extension;
        this.description = description;
        this.icon = Utils.createImageIcon(icon);
    }

    public int getIndex()
    {
        return index;
    }

    public String getSearch()
    {
        return search;
    }

    public String getType()
    {
        return type;
    }

    public String getExtension()
    {
        return extension;
    }

    public String getDescription()
    {
        return description;
    }

    public ImageIcon getIcon()
    {
        return icon;
    }
}

自定义ArrayList对象即保持数据。

The Custom ArrayList Object which maintains the data.

import java.util.AbstractList;
import java.util.ArrayList;

import javax.swing.ImageIcon;

public class FileList extends AbstractList
{
    private ArrayList<FileType> fileList;

    public FileList()
    {
        fileList = new ArrayList<FileType>();
    }

    public void add(String search, String type, String extension, String description, String icon)
    {

        FileType data = new FileType(fileList.size(), search, type, extension, description, icon);
        if (!fileList.contains(data))
        {
        fileList.add(data);
        }
    }

    @Override
    public Object get(int index)
    {
        return fileList.toArray()[index];
    }

    public int getIndex(String search)
    {
        for (FileType obj : fileList)
        {
            if ((obj.getSearch()).equalsIgnoreCase(search))
                return obj.getIndex();
        }
        return -1;
    }

    public String getType(String search)
    {
        for (FileType obj : fileList)
        {
            if ((obj.getSearch()).equalsIgnoreCase(search))
                return obj.getType();
        }
        return "";
    }

    public String getExtension(int index)
    {
        for (FileType obj : fileList)
        {
            if (obj.getIndex() == index)
                return obj.getExtension();
        }
        return "";
    }

    public String getExtension(String search)
    {
        for (FileType obj : fileList)
        {
            if ((obj.getSearch()).equalsIgnoreCase(search))
                return obj.getExtension();
        }
        return "";
    }

    public String getDescription(String search)
    {
        for (FileType obj : fileList)
        {
            if ((obj.getSearch()).equalsIgnoreCase(search))
                return obj.getDescription();
        }
        return "";
    }

    public ImageIcon getIcon(String search)
    {
        for (FileType obj : fileList)
        {
            if ((obj.getSearch()).equalsIgnoreCase(search))
                return obj.getIcon();
        }
        return null;
    }

    @Override
    public int size()
    {
        return fileList.size();
    }
}

而你会被称之为:

And you would call this by:

    FileList list = new FileList();
    list.add("html", "random desc html", Utils.html, "ASCII HTML Files", "images/html.png");
    list.add("bootstrap.html", "random desc bootstrap", Utils.bootstrap, "Bootstrap HTML Files",
            "images/bootstrap.png");
    list.add("xml", "random desc xml", Utils.xml, "XML Files", "images/xml.png");
    list.add("json", "random desc json", Utils.json, "JSON Files", "images/json.png");
    list.add("pdf", "random desc pdf", Utils.pdf, "PDF Documents", "images/pdf.png");
    list.add("doc", "random desc doc", Utils.doc, "Google Documents", "images/doc.png");

这些类创建添加自定义过滤器,查看条件和图标时,JFileChooser的扩充成为一个更面向对象的方式。

These classes were created to augment JFilechooser into a more OO manner when adding custom filters, view conditions and icons.

我相信,当涉及到性能和散列映射将是一个比较理想的选择,但我的目的和时间限制这样做我想要的工作,这并不是最佳选择:)

I am sure this is not optimal when it comes to performance and a hash map would be a more ideal choice but for my purposes and time constraints this does the job i want :)

推荐答案

您可以使用的HashMap 在这里你映射了所需属性的对象。如果你想使用多个属性来获取你的对象,你可以创建多个的HashMap 。这种方法的问题是,你最终有一个映射您要照顾每个属性,你只能得到每个键的对象。

You can use a HashMap where you map the desired property to the object. If you want to get your objects using more than one property, you can create multiple HashMap. The problem of this approach is that you end up with one map for each property you want to look after and you can only get object per key.

如果你正在寻找的排序,我的建议是使用 TreeMap的和反复折腾比较接口,它允许实施多个属性比较。

If you're looking for sorting, my suggestion is to use a TreeMap and fiddle around with the Comparatorinterface, which allows to implement comparators for multiple properties.

编辑:
因为可能有太多的属性,以照顾其目标是不排序,也许是最好的办法是使用的ArrayList 通过添加的检查,以保持类,检查是否文件有一个指定的属性值。
它的工作原理就像比较接口(这里的泛型可选):

Since there may be too many properties to look after and the goal is not sorting, maybe the best way is to keep using ArrayList with the addition of Checker classes, which check if a File has a specified property value. It works like the Comparator interface (the generics here are optional):

public interface Checker<T>
{
    public boolean hasProperty(T o);
}

检查类的一个实例找到文件按名称:

An example of a Checker class to find a File by its name:

public class FileNameChecker implements Checker<File>
{
    private String name;

    public FileNameChecker(String name) {
        this.name = name;
    }

    @Override
    public boolean hasProperty(File f) {
        return f.getName().equals(name);
    }

}

而在你的文件系统中,一般找到方法:

public File find(Checker<File> checker) {
    for(File f : fileList) {
        if(checker.hasProperty(f))
            return f;
    }
    return null;
}

和与调用它:

find(new FileNameChecker("Filename"))

请注意,您可以轻松地修改找到返回多个文件

Note that you can easily modify find to return more than one File.

如果您选择跟随这个解决方案,我建议你看一看的Java 8 lambda表达式和方法,即过滤器。这些基本缓解这一切的过程。

If you chose to follow this solution, I suggest you take a look at Java 8 lambdas and Stream methods, namely filter. These basically ease all this process.

这篇关于在ArrayList和检索的自定义对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆