strings and ints stored in one array

strings and ints stored in one arrayis this possible? how would you declare it?
[93 byte] By [ryankluga] at [2007-11-26 23:55:50]
# 1
Use a collection, such as a java.util.List or java.util.ArrayList
Navy_Codera at 2007-7-11 15:40:19 > top of Java-index,Java Essentials,New To Java...
# 2

If using Java 5+, you could declare an array of objects (thanks to autoboxing) ...

class Arrays {

public static void main(String[] argv) {

Object[] objects = new Object[4];

objects[0] = 123;

objects[1] = "Caleb Payne";

objects[2] = 12.34;

objects[3] = 'c';

for( Object o : objects ) System.out.println(o);

}

}

Navy_Codera at 2007-7-11 15:40:19 > top of Java-index,Java Essentials,New To Java...
# 3

well see im working on a problem that asks the user to input a users first name(string), last name(string) and zip code(int) then at the end I have to sort them in increasing order by zip code.

Im trying to even just put each of them in an array but when i get to sorting them, im going to be in trouble.

Message was edited by:

ryanklug

ryankluga at 2007-7-11 15:40:19 > top of Java-index,Java Essentials,New To Java...
# 4

So then we'll take the object oriented approach:

import java.util.Arrays;

import java.util.Scanner;

class UserInformation implements Comparable {

private String firstName;

private String lastName;

private int zip;

public UserInformation() { }

public UserInformation(String firstName, String lastName, int zip) {

this.firstName = firstName;

this.lastName = lastName;

this.zip = zip;

}

public void setFirstName(String firstName) {

this.firstName = firstName;

}

public void setLastName(String lastName) {

this.lastName = lastName;

}

public void setZIP(int zip) {

this.zip = zip;

}

public String getFirstName() {

return firstName;

}

public String getLastName() {

return lastName;

}

public int getZIP() {

return zip;

}

public boolean equals(UserInformation ui) {

return this.zip == ui.getZIP();

}

public String toString() {

return "First Name = " + firstName

+ "\nLast Name = " + lastName

+ "\nZIP = " + zip;

}

public int compareTo(Object ui) {

UserInformation uia = (UserInformation)ui;

return this.getZIP() - uia.getZIP();

}

}

public class TestClass {

private UserInformation[] users;

private Scanner scanner;

private String fn;

private String ln;

private int zip;

public TestClass() {

users = new UserInformation[3];

scanner = new Scanner(System.in);

for( int i = 0; i < 3; i++ ) {

System.out.println("Record " + i);

System.out.println("--");

System.out.print("First name: ");

fn = scanner.next();

System.out.print("Last name: ");

ln = scanner.next();

System.out.print("ZIP: ");

zip = scanner.nextInt();

System.out.println();

users[i] = new UserInformation(fn, ln, zip);

}

Arrays.sort(users);

for( UserInformation ui : users ) System.out.println( ui );

}

public static void main(String[] argv) {

new TestClass();

}

}

Navy_Codera at 2007-7-11 15:40:19 > top of Java-index,Java Essentials,New To Java...