java Java数组记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28607405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java array records
提问by Mr.Pengu
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...
我需要获得一个具有 3 种类型值的变量类型,我知道的唯一方法是“记录”,但我知道 Java 中没有“记录”这样的类型ArrayList
,但是我不明白...
My result should be: (Don't know how it would look in Java, so I'm showing in different style)
我的结果应该是:(不知道它在 Java 中的样子,所以我以不同的风格展示)
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?
关于如何制作这样的东西的任何建议?ArrayList
那真的是我需要的吗?
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);
}
}
回答by glglgl
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.
AnArrayList
是数组的灵活替代方案,用于存储“一堆”具有相同类型的数据。
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
在 Java 中这样做的标准方法是定义一个类
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")));
}
回答by Razib
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
.
在这里salary
,bonus
和YesOrNo
所有这些都是数据int
,float
并char
为数据类型。我们叫的salary
是一种int
类型的数据,bonus
是一种float
类型的数据。在这里int
,float
,char
-这些数据类型是基本类型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 -
除了原始类型之外,您还可以object oriented
使用诸如 - 之类的编程语言创建自己的数据类型java
。在这里,我认为您可以创建一个命名类Record
或使用其他更有意义的名称-
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
-
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.
然后在OOP
我们调用新创建的类——Record
作为用户定义的数据类型。现在我们可以将其aRecord
称为Record
类型数据。
Then you can use ArrayList
to store the Record
type data -
然后你可以ArrayList
用来存储Record
类型数据 -
List<Record> recordList = new ArrayList<Recored>();