May 04

Following shows how to use a loop to determine the breakdown of change for a given amount, minimising the number of notes and coins used.

		int[] denominations = { 500, 200, 100, 50, 20, 10, 5, 2, 1 };
		int amount = 687;
		int[] count = new int[denominations.length];
		for (int i=0; i<denominations.length; i++) {
			while (amount>=denominations[i]) {
				count[i]++;
				amount -= denominations[i];
			}
		}
		System.out.println(Arrays.toString(count));

written by objects \\ tags: , , ,

Apr 20

The mapping configuration can vary a little depending on the exact relationship involved but the following example should give you an isea what is required. In this example there is a one-to-many relationship to the objects in the array.

The Parent class has array of Child instances as a property.

public class Parent {
	private Integer id;
	private Child[] array;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Child[] getArray() {
		return array;
	}

	public void setArray(Child[] array) {
		this.array = array;
	}
}

The Child class in this example is just a simple bean.

public class Child {
	private Integer id;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}
}

The required mapping is shown in the following xml snippet.


<class name="Parent">

	<id name="id">
		<generator class="increment"/>
	</id>

	<array name="array" cascade="all" fetch="join"  >
		<key column="aid"/>
		<list-index column="id"/>
		<one-to-many class="Child"/>
	</array>
</class>

<class name="Child" lazy="true">
	<id name="id">
		<generator class="increment"/>
	</id>
</class>

If I get some time I’ll try and also post how to specify the mapping using hibernate annotations.

written by objects \\ tags: , ,

Feb 05

The Arrays class has a set of helper toString methods for converting an array to a string representation.

String representation = Arrays.toString(array);

This will work for all array types. For Objects it uses the toString() method of the type to convert array elements.

If you need more control how the array is represented then you will need to implement the conversion yourself using a loop.

written by objects \\ tags: , , ,