Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 访问类的实例列表_Java_List_Class_Arraylist - Fatal编程技术网

Java 访问类的实例列表

Java 访问类的实例列表,java,list,class,arraylist,Java,List,Class,Arraylist,我是Java新手,我有一个关于如何详细访问类的实例列表的问题。(我真的需要你的帮助!) 假设我们有一对数字,比如(12),(34),(56),(53),(15)。 对于每一对,我们认为头一个为头,最后一个为尾(如1个>头部和2个>尾)。每次从输入中获得一对或节点时,我们都会将该对存储到节点列表中 这是我想问的问题。 是否有其他方法仅从节点列表访问头部??例如,考虑具有此对(1 5)的最后一个节点。我们将1称为lastnode.head,将5称为lastnode.tail。 我现在要做的是检查la

我是Java新手,我有一个关于如何详细访问类的实例列表的问题。(我真的需要你的帮助!)

假设我们有一对数字,比如(12),(34),(56),(53),(15)。 对于每一对,我们认为头一个为头,最后一个为尾(如1个>头部和2个>尾)。每次从输入中获得一对或节点时,我们都会将该对存储到节点列表中

这是我想问的问题。 是否有其他方法仅从节点列表访问头部??例如,考虑具有此对(1 5)的最后一个节点。我们将1称为lastnode.head,将5称为lastnode.tail。 我现在要做的是检查lastnode.head(1)是否在节点列表中。在节点列表中,我们已经有(12)个节点。然后我们将(12)存储到一些DS中。之后,在这一次中,我们检查节点列表中的lastnode.tail(5)。注意,我们有两对(5 6)和(5 3),当我们检查时,我们只考虑NODLIST中的头。由于(56)比(53)先出现,我们依次存储(56)和(53)


对这个问题有什么建议或想法??提前谢谢。

我想你们有结对课,比如说:

class MyPair {
    public int head, tail;
    
    public MyPair(int head, int tail) {
        this.head = head;
        this.tail = tail;
    }

    ...
}
boolean containsHead(List<MyPair> list, int value) {
    for(MyPair pair : list) {
        if(pair.head == value) {
            return true;
        }
    }
    return false;
}
List<Integer>
    heads = new ArrayList<>(),
    tails = new ArrayList<>();

heads.add(1);
tails.add(2);
...
非常标准的方法是使用for循环,并根据您要查找的内容比较每对变量。例如,假设您创建了一个填充了一些元素的ArrayList:

List<MyPair> myList = new ArrayList<>();
myList.add(new MyPair(1, 2));
myList.add(new MyPair(3, 4));
...
你们可以用非常相似的方法来处理尾巴

另一种方法是更改您的结构,例如,您可以使用“列表对”,而不是使用对列表,创建两个列表,如下所示:

class MyPair {
    public int head, tail;
    
    public MyPair(int head, int tail) {
        this.head = head;
        this.tail = tail;
    }

    ...
}
boolean containsHead(List<MyPair> list, int value) {
    for(MyPair pair : list) {
        if(pair.head == value) {
            return true;
        }
    }
    return false;
}
List<Integer>
    heads = new ArrayList<>(),
    tails = new ArrayList<>();

heads.add(1);
tails.add(2);
...
我想到的最后一种方法(但我不建议使用)是重写equals方法,使您的类的行为与您设置它的任一项相同

class MyPair {
    public int head, tail;
    
    public MyPair(int head, int tail) {
        this.head = head;
        this.tail = tail;
    }
    
    private static boolean asHead = true;
    
    public static MyPair searchOf(int x) {
        return new MyPair(x, x);
    }
    
    public static void behaveAsHead() {
        asHead = true;
    }
    
    public static void behaveAsTail() {
        asHead = false;
    }
    
    @Override
    public boolean equals(Object other) {
        if (other instanceof MyPair) {
            MyPair otherPair = (MyPair) other;
            return asHead ? (head == otherPair.head) : (tail == otherPair.tail);
        }
        return false;
    }
}
用法:

containsHead(myList, 3);
>> true
tails.contains(2);
>> true
// search for tail of value 4
MyPair.behaveAsTail();
myList.contains(MyPair.searchOf(4));
>> true