Term over-type structure = a data structure that accepts different types, can be primitive or user-defined.
I think ruby supports many types in structures such as tables. I tried a table with types 'String', 'char' and 'File' in Java but errs.
- How can I have over-typed structure in Java?
How to show types in declaration? What about in initilization? Suppose a structure:
INDEX VAR FILETYPE //0 -> file FILE //1 -> lineMap SizeSequence //2 -> type char //3 -> binary boolean //4 -> name String //5 -> path String
Code
import java.io.*;
import java.util.*;
public class Object
{
public static void print(char a)
{
System.out.println(a);
}
public static void print(String s)
{
System.out.println(s);
}
public static void main(String[] args)
{
Object[] d = new Object[6];
d[0] = new File(".");
d[2] = 'T';
d[4] = ".";
print(d[2]);
print(d[4]);
}
}
Errors
Object.java:18: incompatible types
found : java.io.File
required: Object
d[0] = new File(".");
^
Object.java:19: incompatible types
found : char
required: Object
d[2] = 'T';
^
After the nuisance to the real problem:
d[2] stores char-type, but methods sees it as Object. Many of my methods do not have Object, so changing them because of this one feels too much.
- How can I change the types before giving them as pars?
- Should I do it in separate processing class or is there ready method?
Code
package file;
import java.io.*;
import java.util.*;
public class ObjectTest
{
//I have this kind of methods
//I want them to work with Object
// without changing the par type,
// possible?
public static void print(char a)
{
System.out.println(a);
}
public static void print(String s)
{
System.out.println(s);
}
public static void main(String[] args)
{
java.lang.Object[] d = new java.lang.Object[6];
d[0] = (Object) new File(".");
d[2] = (Object) new Character('T');
d[4] = (Object) new String(".");
print(d[2]);
print(d[4]);
}
//I can get it this way working
// but some of my methods are not Objects
// and they need to be types like String
private static void print(Object object) {
System.out.println(object);
}
}