Java 固定大小数组的Set方法和Get方法
对于如何为固定数组应用“set”和“get”方法,我有点搞混了。以下是我在Netbeans方面的一些工作:Java 固定大小数组的Set方法和Get方法,java,arrays,Java,Arrays,对于如何为固定数组应用“set”和“get”方法,我有点搞混了。以下是我在Netbeans方面的一些工作: //creating 5 fixed arrays of size 10 private String [] itemnames = new String [10]; private String [] itemcodes = new String [10]; private String [] category = new String [10]; private String [] q
//creating 5 fixed arrays of size 10
private String [] itemnames = new String [10];
private String [] itemcodes = new String [10];
private String [] category = new String [10];
private String [] quantity = new String [10];
private Double [] sellingprice = new Double [10];
//initialising each array to null in the class constructor
for (int i = 0; i < 10; i++){
itemnames[i] = "";
}
for (int i = 0; i < 10; i++){
itemcodes[i] = "";
}
for (int i = 0; i < 10; i++){
category[i] = "";
}
for (int i = 0; i < 10; i++){
quantity[i] = "";
}
for (int i = 0; i < 10; i++){
(Double.parseDouble(sellingprice[i])) = 0;
}
//创建5个大小为10的固定数组
私有字符串[]itemnames=新字符串[10];
私有字符串[]itemcodes=新字符串[10];
私有字符串[]类别=新字符串[10];
私有字符串[]数量=新字符串[10];
私人双[]售价=新双[10];
//在类构造函数中将每个数组初始化为null
对于(int i=0;i<10;i++){
项目名称[i]=“”;
}
对于(int i=0;i<10;i++){
项目代码[i]=“”;
}
对于(int i=0;i<10;i++){
类别[i]=“”;
}
对于(int i=0;i<10;i++){
数量[i]=“”;
}
对于(int i=0;i<10;i++){
(Double.parseDouble(sellingprice[i])=0;
}
现在,我被困在每个数组的set方法和get方法中。需要帮忙吗
谢谢:)您可以根据您想对数组执行的操作(或以后能够执行的操作)来创建set和get方法 如果希望能够将数组检索到另一个类中,可以创建如下get方法:
public String[] getItems()
{
return itemnames;
}
public String getItemMatchingCode(String code)
{
for(int i = 0; i < ARR_LENGTH; i++)
{
if(code.equals(itemcodes[i]) return itemnames[i];
}
}
另一方面,如果您只希望其他类获取数组中的特定项,则一种方法可能如下所示:
public String[] getItems()
{
return itemnames;
}
public String getItemMatchingCode(String code)
{
for(int i = 0; i < ARR_LENGTH; i++)
{
if(code.equals(itemcodes[i]) return itemnames[i];
}
}
旁注:
您没有在类构造函数中将每个数组“//初始化为null”,默认情况下,它们是空的。您所做的是用空字符串填充它们,这在大多数情况下是不必要的
当遍历数组并用值填充它们时,可以在一个循环中完成所有操作
for (int i = 0; i < 10; i++)
{
itemnames[i] = "";
itemcodes[i] = "";
category[i] = "";
}
“每个数组的set方法和get方法”是什么意思?完全不清楚您想要实现什么。如果您传入索引和值,那么您会将该位置的元素设置为值。