Solved

Container for different primitive types and type casting

Posted on 2009-05-17
8
580 Views
Last Modified: 2013-11-12
Using java 6, I want to create a class that will contain a two dimensional arrays of different types. It should work for int, double and boolean types, and it should have methods for getting this array as another type.

I tried doing this using generics, but I'm not sure if this is the best way. When using generics i'm not able to use primitive types. And when I use wrapper classes type casting is not that easy. I cannot cast from Integer to Double.

Any suggestions to how I can do this a better way?

public class ArrayContainer<T>
{
 
	protected T[][] array;
 
	public ArrayContainer( T[][] array)
	{
		this.array = array;
	}
 
	public T[][] getArray()
	{
		return array;
	}
 
	public void setArray( T[][] pattern )
	{
		this.pattern = pattern;
	}
	
	public Double[][] getArrayAsDouble()
	{
		if (arrayinstanceof Double[][])
		{
			return (Double[][]) array;
		}
		else if (array instanceof Integer[][])
		{
			return (IntegerToDouble(array));
		}
                .....
 
		return null;
	}
 
       public int getArraryAsInt()
       {
       }
	protected Double[][] IntegerToDouble(T[][] values) 
	{
	    ....
	}
	.....
}

Open in new window

0
Comment
Question by:jstakk
  • 4
  • 2
  • 2
8 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 24405881
You can't cast arrays. You'd need to create an array of the correct type and copy the elements
0
 
LVL 2

Author Comment

by:jstakk
ID: 24405903
Yes I know. But I still can't cast each element directly from Integer to Double.

This should be done to very large arrays, so it has to be efficient.
Integer i = new Integer(1000);
Double d = (double) (int) i;

Open in new window

0
 
LVL 3

Expert Comment

by:hazgoduk
ID: 24405910
You can do new Double(i) surely...?

Or you could have Class arrayClass = Object.class and have a setClass(Class class) method. Then just call setClass(Integer.class) and in your setValues you can check the values being entered are instanceof class
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 24405928
I think perhaps you have something like this in mind (always trickyish but works)?
public class ArrayContainer<T> {
    protected T[][] array;
 
    public ArrayContainer(T[][] array) {
        this.array = array;
    }
 
    public T[][] getArray() {
        return array;
    }
 
    public void setArray(T[][] array) {
        this.array = array;
    }
 
    public Double[][] getArrayAsDouble() {
        Double[][] result = new Double[array.length][];
	for(int i = 0;i < result.length;i++) {	
	    result[i]  = new Double[array[i].length];
	    for(int j = 0;j < result[i].length;j++) {
		result[i][j] = ((Number)array[i][j]).doubleValue();
	    }
	}
        return result;
    }
 
    public static void main(String[] args) {	
	Integer[][] ii  = { 
	    { 1, 2, 3, 4 },
	    { 5, 6, 7, 8}
	};
	ArrayContainer ac = new ArrayContainer<Integer>(ii);
	Double[][] conv = ac.getArrayAsDouble();
	for (Double[] a : conv) {
	    System.out.println(java.util.Arrays.toString(a));
	}
    }
}

Open in new window

0
 
LVL 2

Author Comment

by:jstakk
ID: 24405955
@hazgoduk: The constructor for Double takes either a double, or a String as argument. Using an Integer in the constructor will work but it will call the Integer's toString() method and then parse the string. Not very efficient. Your second suggestion is perhaps a good way to go, i'll look into that. Thank you.

@CEHJ:
Thats what I was looking for. Looks like a good solution to my problem. I'm gonna try it out.

Thank you both very much for you quick and good help.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24405967
Why would you need a 'setClass' method?
0
 
LVL 3

Expert Comment

by:hazgoduk
ID: 24405972
I must admit I didn't look at the code very much as I just woke up! You would indeed not need a setClass method. Just set the class based on the type sent in the contructor
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24406381
:-)
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
map related example 6 55
hibernate example for saving data 19 57
Convert from a json string array to a Java object 3 54
What browser will run Java? 7 69
The Fluent Interface Design Pattern You can use the Fluent Interface (http://en.wikipedia.org/wiki/Fluent_interface) design pattern to make your PHP code easier to read and maintain.  "Fluent Interface" is an object-oriented design pattern that r…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question