Java: over-typed structures?
Posted
by HH
on Stack Overflow
See other posts from Stack Overflow
or by HH
Published on 2010-05-06T21:05:28Z
Indexed on
2010/05/06
21:08 UTC
Read the original article
Hit count: 202
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';
^
© Stack Overflow or respective owner