Java array records
Asked Answered
A

2

5

I need to get an variable type that has 3 types of values, the only way I know to do that is "Records", but I know there is no such type as "Records" in Java but ArrayList, but I don't get it...

My result should be: (Don't know how it would look in Java, so I'm showing in different style)

TData = Record
     Username : String;
     UserID : Int ;
     RowID : Int;
   end;
users : array[1..10] of TData;

for(int i = 1; i < rowCount; i++){
   TData.Username[i] = rs.GetString("Name");
   TData.UserID[i] = rs.GetInt("UserID");
   TData.RowID[i] = row;
}

Any suggestions on how to make something like this? Is ArrayList that I really need?

Thanks for the help, at the end i combined and got this result, works perfectly:

class MyData {
        private String userName;
        private int userID;
        private int RowID;


        public MyData(String userName, int userID, int RowID) {
            this.userName = userName;
            this.userID = userID;
            this.RowID = RowID;
        }

        public String getUserName() {
            return userName;
        }

        public int getUserID() {
            return userID;
        }
        public int getRowID() {
            return RowID;
        }
    }

    public void AList(){
        ArrayList<MyData> users = new ArrayList<>();
        try{
            conn = DBConnection.DBConnector();
            pst = conn.prepareStatement("SELECT * FROM TableUserData");
            rs = pst.executeQuery();
            row = 0;
            while (rs.next()) {
                users.add(new MyData(rs.getString("User_name"), rs.getInt("ID"), row));
                row++;
                }
            for (MyData tmp : users) {
                JOptionPane.showMessageDialog(null, "Users: " + tmp.getUserName() + " row: " + rtmp.getRowID());

            }

        }catch(Exception e){
            JOptionPane.showMessageDialog(null, "Arraylist error: " + e);
        }

    }
Aeroneurosis answered 19/2, 2015 at 13:16 Comment(4)
You could make your own Record class and give it those fields.Shovelboard
Instead of records, you use classes in Java. Java has arrays, which are same as in Pascal. ArrayList is a List, which using Array for storing items, it is not array itself (however it is usually better to use, because you dont have to set the size of it, it is resizing automatically)Pluralize
@glglgl OP has said that don't know how it would look in java so, i'm showing in different style How can you change his pseudo-code to yours? And certainly there are more important things to edit.Sonnnie
@Sonnnie This pseudo code is clearly inspired by Pascal, and even it it wouldn't, there would be no point to access fields of the type, so I changed that, taking care that I don't change the intention of all. If you see other things worth to be changed, feel free to do so.Kleist
K
8

You are mixing up things.

An ArrayList is a flexible alternative to an array and serves to store "a bunch" of data which have the same type.

The type of these data (of one entry) is up to you.

The standard way of doing this in Java is to define a class like

class MyData {
    private String userName;
    private int userID;
    private int rowID;

    public MyData(String userName, int userID, int rowID) {
        this.userName = userName;
        this.userID = userID;
        this.rowID = rowID;
    }

    public String getUserName() {
        return userName;
    }

    public int getUserID() {
        return userID;
    }

    public int getRowID() {
        return rowID;
    }
}

and then

ArrayList<MyData> users = new ArrayList<>();

while (rs.next()) {
   users.add(new MyData(rs.GetString("Name"), rs.GetInt("UserID"), rs.GetInt("RowID")));
}
Kleist answered 19/2, 2015 at 13:20 Comment(1)
you forgot to add the arguments to the constructor.Nightfall
M
2

Consider the following code snippet -

int salary;
float bonus;
char YesOrNo = 'Y';  

Here salary, bonus and YesOrNo all of these are data and int, float and char are data type. We called salary is an int type data, bonus is a float type data. Here int, float, char - these data type are of primitive type in java.

In addition to primitive type you can create your own data type in object oriented programming language like - java. Here I think you can create a class of named Record or with some other more meaningful name -

public class Record {
    private String userName;
    private int userId; 
    private int rowId;

    public Record(String userName, int userId, int rowId){
      this.userName = userName;
      this.userId = userId;
      this.rowId
    }

    //getter and setter methods  if required
}  

After that when you create a instance/object of Record -

Record aRecord = new Record("user001", 1, 1);

Then in OOP we called the newly created class - Record as a user defined data type. Now we can called aRecord as a Record type data.

Then you can use ArrayList to store the Record type data -

List<Record> recordList = new ArrayList<Recored>();
Merit answered 19/2, 2015 at 13:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.