如何使用equals方法将对象添加到数组列表以排除类似对象?(How can I add objects to an array list using the equals method to exclude similar objects?)

我正在尝试创建Line对象并将它们添加到数组列表中。 我遇到的问题是排除任何彼此相似的行。 我已经创建了一个equals方法,它比较两行以确定它们是否相等。 我在使用while循环时遇到问题。 我没有错误消息。 它编译得很好。 它只是不会从文本文件中读取。 我被困住了,不知道从这里可以去哪里。

public void read( File fileName ) throws Exception { reader = new Scanner(fileName); //--------------------- //Have to read the first number before starting the loop int numLines = reader.nextInt(); lines = new ArrayList <Line> (numLines); //This loop adds a new Line object to the lines array for every line in the file read. while( reader.hasNext() ) { for( int i = 0; i < numLines; i++ ) { int x = reader.nextInt(); int y = reader.nextInt(); Point beg = new Point(x,y); x = reader.nextInt(); y = reader.nextInt(); Point end = new Point(x,y); String color = reader.next(); Line l = new Line( beg, end, color ); if (l.equals(lines.get(i))) break; else lines.add(i, l); } } //Print the action to the console System.out.println( "reading text file: " + fileName ); reader.close(); }

I am trying to create Line objects and add them to an array list. The problem I am having is excluding any lines that are similar to each other. I have already created an equals method that compares two lines to determine if they are equal. I am having trouble using the while loop. I do not have an error message. It compiles just fine. It just will not read from the text file. I am stuck and do not know where else to go from here.

public void read( File fileName ) throws Exception { reader = new Scanner(fileName); //--------------------- //Have to read the first number before starting the loop int numLines = reader.nextInt(); lines = new ArrayList <Line> (numLines); //This loop adds a new Line object to the lines array for every line in the file read. while( reader.hasNext() ) { for( int i = 0; i < numLines; i++ ) { int x = reader.nextInt(); int y = reader.nextInt(); Point beg = new Point(x,y); x = reader.nextInt(); y = reader.nextInt(); Point end = new Point(x,y); String color = reader.next(); Line l = new Line( beg, end, color ); if (l.equals(lines.get(i))) break; else lines.add(i, l); } } //Print the action to the console System.out.println( "reading text file: " + fileName ); reader.close(); }

最满意答案

Java Collection有很多东西要发现。 您使用了错误的数据结构,可以在List添加两个不同的对象,因为List的目的是:

有序集合(也称为序列)。 该接口的用户可以精确控制列表中每个元素的插入位置。 用户可以通过整数索引(列表中的位置)访问元素,并搜索列表中的元素。

因此,您在添加对象时保留给定顺序中的元素,并且您可以按此顺序访问任何给定索引处的对象。

现在看来这不是你想要的,你宁愿没有重复的元素而不是订单,对吧? 如果是这样,您需要使用实现Set接口的类,其目的是:

不包含重复元素的集合。 更正式地说,集合不包含元素对e1和e2,使得e1.equals(e2)和至多一个null元素。 正如其名称所暗示的,此接口模拟数学集抽象。

java框架包含一组的两个实现:

HashSet :它是一个基于哈希的实现,享受散列的好处,确保无论您的集合大小如何,都可以获得持续的访问时间 TreeSet :它是一个基于树的实现,具有基本操作的log(n)时间。

我建议您查看我给出的第一个链接,它是oracle教程,详细解释了Java Collections。

你的例子与集

它非常简单,并且使用ArrayList并不是那么远。

将List的声明更改为类似的Set (我使用了TreeSet但您可以使用Set任何其他实现):

Set<Line> lines = new TreeSet<Line>();

当您想要填充集合并让它完成工作时,只需使用Set界面的add(E e)功能:

Line l = new Line(beg.x, beg.y, end.x, end.y); lines.add(l);

如果您仍想使用List

您可以使用contains(Object o)方法检查元素是否在List (或任何其他Collection中contains(Object o) 。

lines.contains(l)

如果新创建的Line ( l )包含在您的集合( lines )中,则返回true。

There is a lot to discover in the Java Collection. You are using the wrong data structure, you can add two different objects in a List because the purpose of the list is to be :

An ordered collection (also known as a sequence). The user of this interface has precise control over where in the list each element is inserted. The user can access elements by their integer index (position in the list), and search for elements in the list.

So you have the elements in a given order which is kept when adding objects and you can access objects at any given index in this order.

Now it seems that's not what you want, you'd rather have no duplicate elements rather than an order, right ? If so you need to use a class that implements the Set interface which purpose is to be :

A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element. As implied by its name, this interface models the mathematical set abstraction.

The java framework contains two implementations of a set :

HashSet : It is a hash based implementations, enjoying the benefits of hashing ensuring a constant access time whatever the size of your collection TreeSet : It is a tree based implementation with a log(n) time for basic operations.

I recommend that you look into the first link I gave, it is the oracle tutorial explaining with much details the Java Collections.

Your example with a Set

It's really easy and not that far from using an ArrayList.

Change the declaration of your List to a Set like that (I used a TreeSet but you can use any other implementations of a Set) :

Set<Line> lines = new TreeSet<Line>();

Just use the add(E e) function of the Set interface when you want to populate your collection and let it do the job :

Line l = new Line(beg.x, beg.y, end.x, end.y); lines.add(l);

And if you still want to use a List

You can check if an element is in a List (or any other Collection for that matters) by using the contains(Object o) method.

lines.contains(l)

This will return true if the freshly created Line (l) is contained in your collection (lines).

如何使用equals方法将对象添加到数组列表以排除类似对象?(How can I add objects to an array list using the equals method to exclude similar objects?)

我正在尝试创建Line对象并将它们添加到数组列表中。 我遇到的问题是排除任何彼此相似的行。 我已经创建了一个equals方法,它比较两行以确定它们是否相等。 我在使用while循环时遇到问题。 我没有错误消息。 它编译得很好。 它只是不会从文本文件中读取。 我被困住了,不知道从这里可以去哪里。

public void read( File fileName ) throws Exception { reader = new Scanner(fileName); //--------------------- //Have to read the first number before starting the loop int numLines = reader.nextInt(); lines = new ArrayList <Line> (numLines); //This loop adds a new Line object to the lines array for every line in the file read. while( reader.hasNext() ) { for( int i = 0; i < numLines; i++ ) { int x = reader.nextInt(); int y = reader.nextInt(); Point beg = new Point(x,y); x = reader.nextInt(); y = reader.nextInt(); Point end = new Point(x,y); String color = reader.next(); Line l = new Line( beg, end, color ); if (l.equals(lines.get(i))) break; else lines.add(i, l); } } //Print the action to the console System.out.println( "reading text file: " + fileName ); reader.close(); }

I am trying to create Line objects and add them to an array list. The problem I am having is excluding any lines that are similar to each other. I have already created an equals method that compares two lines to determine if they are equal. I am having trouble using the while loop. I do not have an error message. It compiles just fine. It just will not read from the text file. I am stuck and do not know where else to go from here.

public void read( File fileName ) throws Exception { reader = new Scanner(fileName); //--------------------- //Have to read the first number before starting the loop int numLines = reader.nextInt(); lines = new ArrayList <Line> (numLines); //This loop adds a new Line object to the lines array for every line in the file read. while( reader.hasNext() ) { for( int i = 0; i < numLines; i++ ) { int x = reader.nextInt(); int y = reader.nextInt(); Point beg = new Point(x,y); x = reader.nextInt(); y = reader.nextInt(); Point end = new Point(x,y); String color = reader.next(); Line l = new Line( beg, end, color ); if (l.equals(lines.get(i))) break; else lines.add(i, l); } } //Print the action to the console System.out.println( "reading text file: " + fileName ); reader.close(); }

最满意答案

Java Collection有很多东西要发现。 您使用了错误的数据结构,可以在List添加两个不同的对象,因为List的目的是:

有序集合(也称为序列)。 该接口的用户可以精确控制列表中每个元素的插入位置。 用户可以通过整数索引(列表中的位置)访问元素,并搜索列表中的元素。

因此,您在添加对象时保留给定顺序中的元素,并且您可以按此顺序访问任何给定索引处的对象。

现在看来这不是你想要的,你宁愿没有重复的元素而不是订单,对吧? 如果是这样,您需要使用实现Set接口的类,其目的是:

不包含重复元素的集合。 更正式地说,集合不包含元素对e1和e2,使得e1.equals(e2)和至多一个null元素。 正如其名称所暗示的,此接口模拟数学集抽象。

java框架包含一组的两个实现:

HashSet :它是一个基于哈希的实现,享受散列的好处,确保无论您的集合大小如何,都可以获得持续的访问时间 TreeSet :它是一个基于树的实现,具有基本操作的log(n)时间。

我建议您查看我给出的第一个链接,它是oracle教程,详细解释了Java Collections。

你的例子与集

它非常简单,并且使用ArrayList并不是那么远。

将List的声明更改为类似的Set (我使用了TreeSet但您可以使用Set任何其他实现):

Set<Line> lines = new TreeSet<Line>();

当您想要填充集合并让它完成工作时,只需使用Set界面的add(E e)功能:

Line l = new Line(beg.x, beg.y, end.x, end.y); lines.add(l);

如果您仍想使用List

您可以使用contains(Object o)方法检查元素是否在List (或任何其他Collection中contains(Object o) 。

lines.contains(l)

如果新创建的Line ( l )包含在您的集合( lines )中,则返回true。

There is a lot to discover in the Java Collection. You are using the wrong data structure, you can add two different objects in a List because the purpose of the list is to be :

An ordered collection (also known as a sequence). The user of this interface has precise control over where in the list each element is inserted. The user can access elements by their integer index (position in the list), and search for elements in the list.

So you have the elements in a given order which is kept when adding objects and you can access objects at any given index in this order.

Now it seems that's not what you want, you'd rather have no duplicate elements rather than an order, right ? If so you need to use a class that implements the Set interface which purpose is to be :

A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element. As implied by its name, this interface models the mathematical set abstraction.

The java framework contains two implementations of a set :

HashSet : It is a hash based implementations, enjoying the benefits of hashing ensuring a constant access time whatever the size of your collection TreeSet : It is a tree based implementation with a log(n) time for basic operations.

I recommend that you look into the first link I gave, it is the oracle tutorial explaining with much details the Java Collections.

Your example with a Set

It's really easy and not that far from using an ArrayList.

Change the declaration of your List to a Set like that (I used a TreeSet but you can use any other implementations of a Set) :

Set<Line> lines = new TreeSet<Line>();

Just use the add(E e) function of the Set interface when you want to populate your collection and let it do the job :

Line l = new Line(beg.x, beg.y, end.x, end.y); lines.add(l);

And if you still want to use a List

You can check if an element is in a List (or any other Collection for that matters) by using the contains(Object o) method.

lines.contains(l)

This will return true if the freshly created Line (l) is contained in your collection (lines).