0

Basically i want to create a class "Screws". This class shall describe a Screw by Type, length, thickness etc. I also want to create a "storage" class, that stores said "Screws" in all variances. The type, length etc. is limited to 7 and 3 (so there will be a limited amount of possibilities)

public class Screws{
public Screws(int typ,double durchmesser,double laenge,double gangHoehe){
    this.schraubenArt = typ;
    this.durchmesser=durchmesser;
    this.laenge=laenge;
    this.gangHoehe=gangHoehe;
  }
}

Now i want to create a storage "unit", that will be an arraylist. I want to fill it with objects of "Screws". Afterwards i want to be able to check if a certain Scredobject is contained in the list.

import java.util.ArrayList;
public class Lager {

private Screws schrauben;
private ArrayList<Screws>Kreuzschlitzlager;

public Lager(){
Kreuzschlitzlager = new ArrayList<>();

for(int durchmesser=0; durchmesser <=3; durchmesser++){
    for(int laenge=0; laenge <= 3; laenge++){
        for(int ganghoehe=0; ganghoehe <=3; ganghoehe++){
            Kreuzschlitzlager.add(new Screws (1,durchmesser,laenge,ganghoehe);); 
    }}}}

public boolean checkForObject(Screws object){
    return Kreuzschlitzlager.contains(object);
}

Now my problem is, i get a long list of objects i can "get" the properties of, but if i checkForObject(Anything) it will always give me a "false". I can't figure out why though. Don't worry about the class Screws, it's not nearly done. :D So, how do i check if an array list contains a certain object, why is it not working in this case and can i get the index of the object with .IndexOf?

Shenanigator
  • 17
  • 1
  • 6

4 Answers4

4

You're right; the Screws class isn't done. The ArrayList class calls the equals method to determine if the element is equal to the argument to contains, but you don't override equals. Your Screws class is inheriting equals from Object, and it will only return true if it's the same exact object.

Override the equals method, signature boolean equals(Object obj) in Screws. You will need to code the logic for determining equality yourself.

Additionally, the contract for equals means that it needs to be consistent with hashCode. For this small application, it's not necessary, but generally you should also override hashCode to be consistent with equals.

rgettman
  • 176,041
  • 30
  • 275
  • 357
  • Wait, so i basically have to add a method to screws that compares say getValue1 of the object in the list, to the object created in lager? Thanks a lot, i will try that! – Shenanigator May 28 '15 at 23:32
1

Take a look at the javadoc for List.contains

boolean contains(Object o)

Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)).

Specified by:
contains in interface Collection

Parameters:
o - element whose presence in this list is to be tested Returns: true if this list contains the specified element

Throws:
ClassCastException - if the type of the specified element is incompatible with this list (optional) NullPointerException - if the specified element is null and this list does not permit null elements (optional)

If you don't specify an equals method for your class Screws, it's going to check if they are the same object. Define an equals method.

jgitter
  • 3,396
  • 1
  • 19
  • 26
0

contains method of ArrayList does this:

Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e))

For example, we initialize two Screws objects which contains the exact values.

Screws screws1 = new Screws(1, 1.0, 1.0, 1.0, 1.0);
Screws screws2 = new Screws(1, 1.0, 1.0, 1.0, 1.0);
Kreuzschlitzlager.add(screws1);
Kreuzschlitzlager.contains(screws2);   // this returns false

It only compares the memory address by default. screws1 and screws2 have different memory addresses, so they are not the same object, and they are not equal. You have to override boolean equals(Object) and int hashcode() in your Screws class, define your own way how two Screws objects are equal

Haifeng Zhang
  • 30,077
  • 19
  • 81
  • 125
0

in your Screws class, you need to override the method equals(...) and implement how two Screws should be equal

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80