// how to simulate arrays of mixed types? 
// this programs demonstrates two methods

public class array_mixedtypes2 {

    public static void main(String args[]) {
	
	//------------------------------------------------------
	// METHOD1: create an array of objects
	// Each object contains a field for the desired type
	//------------------------------------------------------
	System.out.println("Method 1:");

	Data[] d = { new Data(1,"a"), new Data(2,"b") };
	
	// If that's too confusing, try instead:
	//     Data[] d = Data[2];
	//     d[0] = new Data(1,"a");
	//     d[1] = new Data(2,"b");
	
	// Print table:
	for(int i = 0; i<=d.length-1; i++)
	    System.out.println(d[i].k + " " + d[i].s);
	
	/* Output:
	   Method 1:
	   1 a
	   2 b
	*/

	//------------------------------------------------------
	// METHOD2: create a 1D array for each different type
	// and remember to print/use in conjunction
	//------------------------------------------------------
	System.out.println("Method 2:");

	int[] a = {1,2};	// initialize array of ints
	String[] b = {"a","b"};	// initialize array of Strings

	// Print table:
	for(int i = 0; i<=a.length-1; i++)
	    System.out.println(a[i] + " " + b[i]);
	
	/* Output:
	   Method 2:
	   1 a
	   2 b
	*/

    } // method main
    
} // class array_mixedtypes2

// Method1 uses the following class:
class Data {
    int k;
    String s;
    Data(int x, String y) {
	k = x;
	s = y;
    } // constructor Data
} // class Data

/* Want to have some fun? 
   Use "this":
class Data {
    int k;
    String s;
    Data(int k, String s) {
	this.k = k;
	this.s = s;
    }
}
*/

