public static void main( String[] args ){ Dog aDog = new Dog("Max"); foo(aDog); if (aDog.getName().equals("Max")) { //true System.out.println( "Java passes by value." ); } else if (aDog.getName().equals("Fifi")) { System.out.println( "Java passes by reference." ); } } public static void foo(Dog d) { d.getName().equals("Max"); // true d = new Dog("Fifi"); d.getName().equals("Fifi"); // true }
@Override public int hashCode() { int result = getName() != null ? getName().hashCode() : 0; result = 31 * result + (getColor() != null ? getColor().hashCode() : 0); result = 31 * result + getWeight(); result = 31 * result + (int) (getLengthInCm() ^ (getLengthInCm() >>> 32)); result = 31 * result + (int) (getHeight() ^ (getHeight() >>> 32)); result = 31 * result + (getSex() != null ? getSex().hashCode() : 0); result = 31 * result + (isNeutered() ? 1 : 0); return result; }
public class NotArrayList { public static void main(String[] args) { Dog[] dogs = new Dog[3]; Dog d1 = new Dog(); d1.setName("aaa"); d1.setAge(1); Dog d2 = new Dog(); d2.setName("bbb"); d2.setAge(1); Dog d3 = new Dog(); d3.setName("ccc"); d3.setAge(1); dogs[0] = d1; dogs[1] = d2; dogs[2] = d3; Dog[] res = Dog.removeByName(dogs, "aaa"); for (Dog d : res) { if (d != null) System.out.println(d.getName()); } } }
@Test public void sanityCheckTestDomain() { Identifier pistachio2Id = new Identifier("Pistachio"); Beagle pistachio2 = (Beagle) new Beagle().name(pistachio2Id).color("tricolor") .sex(MALE).weight(7).length(50l).height(30l).neutered(false); Identifier pistachio3Id = new Identifier("Pistachio 2"); Beagle pistachio3 = (Beagle) new Beagle().name(pistachio3Id).color("tricolor") .sex(MALE).weight(7).length(50l).height(30l).neutered(true); assertNotEquals(pistachio2Id, pistachio3Id); assertNotEquals(pistachio2Id.hashCode(), pistachio3Id.hashCode()); Identifier2 id2 = new Identifier2("22"); Identifier2 id3 = new Identifier2("23"); assertNotEquals(id2, id3); assertNotEquals(id2.hashCode(), id3.hashCode()); assertEquals(pistachio, pistachio2); assertEquals(pistachio.hashCode(), pistachio2.hashCode()); assertNotEquals(pistachio, pistachio3); assertNotEquals(pistachio.hashCode(), pistachio3.hashCode()); pistachio.bay(10, 30); skinny.bay(20,32); juno.herd(); }
listView.setOnItemClickListener(new OnItemClickListener() { public void onClick(AdapterView<?> parent, View view, int position, long id) { //Get your Dog object from the 'position' parameter you get here. //You should save the 'Dogs' in an array, so you can access them here easily. Dog d = mDogsArray[position]; Intent intent = new Intent(YourActivity.this, DogInfoActivity.class); intent.putExtra("name", d.getName()); intent.putExtra("breed", d.getBreed()); intent.putExtra("age", d.getAge()); startActivity(intent); } }
int totalWeight = 0; for (final Dog dog : dogs) { totalWeight += dog.getWeight(); }
private List<Long> getIds(String name, List<?> objects) { List<Long> ids = new ArrayList<Long>(); for (Object object : objects) { if(object instanceof Dog){ Dog dog = (Dog) object; if (dog.getName().equals(name)) ids.add(dog.getId()); } else if (object instanceof Cat){ Cat cat = (Cat) object; if (cat.getName().equals(name)) ids.add(cat.getId()); } } return ids; }
Set<Dog> commonDogsJanuary = new HashSet<Dog>(dogsJanuary); commonDogsJanuary.retainAll(dogsFebruary); Set<Dog> commonDogsFebruary = new HashSet<Dog>(dogsFebruary); commonDogsFebruary.retainAll(dogsJanuary); for (Dog januaryDog : commonDogsJanuary) { int januaryWeight = januaryDog.getWeight(); for (Dog februaryDog : commonDogsFebruary) { if (januaryDog.equals(februaryDog) && januaryWeight > februaryDog.getWeight()) { // Do whatever } }
public void foo(Dog d) { d.getName().equals("Max"); // true d = new Dog("Fifi"); d.getName().equals("Fifi"); // true } Dog aDog = new Dog("Max"); foo(aDog); aDog.getName().equals("Max"); // true In this example aDog.getName() will still return "Max". d is not overwritten in the function as the object reference is passed by value.
public class DogValidator { /* this may extend some abstract validator */ public DogValidator() { super(); } public boolean validatedDog(Dog dog) { /* You can even modify this class to have exclusions e.g. what not to validate (and then use java reflection to invoke methods - may be too advance for what you want to do */ double weight = dog.getWeight(); if(weight > 0 && weight < 500) { return true; } else { return false; } } }
private List getIds(String name, List objects) { List<Long> ids = new ArrayList<>(); for (Object obj : objects) { if (obj instanceof Dog) { Dog d = (Dog) obj; if (d.getName().equals(name)) { ids.add(d.getId()); } } else if (obj instanceof Cat) { Cat c = (Cat) obj; if (c.getName().equals(name)) { ids.add(c.getId()); } } } return ids; }