devger
devger

Reputation: 715

How to organize array with different types?

Hi,

I am new in Java and trying to figure out how to push this data into array(6 rows, 3 columns)?

x1 John 6
x2 Smith 9
x3 Alex 7
y1 Peter 8
y2 Frank 9
y3 Andy 4

Afterwards, I will take numbers from last column for making mathematical calculations.

This is my code...

public class Testing {
    public static void main(String[] args) {

        Employee eh = new Employee_hour();

        Employee_hour [] eh_list = new Employee_hour[6];
        eh_list[0] = new Employee_hour("x1", "John", 6);
        eh_list[1] = new Employee_hour("x2", "Smith", 9);
        eh_list[2] = new Employee_hour("x3", "Alex", 7);
        eh_list[3] = new Employee_hour("y1", "Peter", 8);
        eh_list[4] = new Employee_hour("y2", "Frank", 9);
        eh_list[5] = new Employee_hour("y3", "Andy", 4);
        print(eh_list);
    }

    private static void print(Employee_hour[] mass){
        for (int i = 0; i < mass.length; i++) {
            System.out.print(mass[i] + " ");
        }
        System.out.println();
    }
}

But I'm getting this as the output...

testing.Employee_hour@1a752144 testing.Employee_hour@7fdb04ed testing.Employee_hour@420a52f testing.Employee_hour@7b3cb2c6 testing.Employee_hour@4dfd245f testing.Employee_hour@265f00f9

How can I get numbers from last column?

Upvotes: 1

Views: 274

Answers (5)

maksimov
maksimov

Reputation: 5811

Why not create a specific Java bean for your records?

class Person {
  String id;
  String name;
  int anotherNumber;
  // constructor, getters, setters
}

And then use it like this:

Person [] people = new Person[10];
people[0] = new Person("x1", "John", 6);
...

Or better yet employ java.util.List instead of the array.

Field Access

In order to access separate fields, you either need to make your fields public (very bad idea) and simply refer to them as object_instance.field_name, or provide so-called getters:

class Person {
  String id;
  String name;
  int anotherNumber;
  // constructor, getters, setters

  public int getAnotherNumber() {
     return anotherNumber;
  }
}

Then call it when printing:

for (int i = 0; i < mass.length; i++) {
    System.out.print(mass[i].getAnotherNumber() + " ");
}

Why what you tried didn't work:

System.out.println(mass[0]) in your case will print the whole object representation, by default it prints what it did in your case. To do it nicer you need to override Object's String toString() method:

class Person {
  String id;
  String name;
  int anotherNumber;
  // constructor, getters, setters

  public String toString() {
     return "{id="+id+", name="+name+", anotherNumber="+anotherNumber+"}";
  }
}

Upvotes: 9

Takiro
Takiro

Reputation: 470

There are some ways to store your data in an array but you can only store values of the same type in one Array. For Example just String, int or float. In your case you have to use String but you can't do any calculations with a string type variable even if it contains a number. I would suggest the way maksimov describes.

Upvotes: 0

Akhi
Akhi

Reputation: 2242

If you want to store it as array user 2 dimentional array.Here is a sample.

String[][] a2 = new String[10][5];
   for (int i=0; i<a2.length; i++) {
   for (int j=0; j<a2[i].length; j++) {
   a2[i][j] = i;
   System.out.print(" " + a2[i][j]);
   }
   System.out.println("");
   }
  }

The better approach would be to cretae an Object

class User {
  String id;
  String userName;
  int userSomeValue;

 //
}

Then push it to a list

User ob1=new User();
// set the values.
List<User> userList=new ArrayList<User>();
userList.add(ob1);

Use this list for procesing by retriving the contents using

userList.get(index);

Upvotes: 2

amicngh
amicngh

Reputation: 7899

Array is a type-safe collection you can use array of generic objects. Other way use collection Api.

Upvotes: 1

Java is strongly typed, so you can't just make an array that will accept any type.
However, you could make a multidimensional array of type Object, and use java.lang.Integer for the integer values.
The alternative is to make a class that represents the rows in your table, and make an array of that class.

Upvotes: 3

Related Questions