英文:
Why is every parameter of my "Services" Object working but this one?
问题
我正在创建一个由我创建的 Services 对象组成的 Array List differentServices。
当我尝试从文件中获取数据,该文件将作为带有服务名称、服务文件名和服务费用参数的服务,并在打印数组列表 differentService 的值时,我注意到数组列表中的服务名称都是不同的,并且根据文件,数组列表中不同对象的服务费用也都是不同的,而且根据文件,但是 differentServices 中每个对象的文件名都是相同的,即使我声明新的 Services() 的方式与服务名称和服务费用相同。
我试图理解为什么,因为我需要每个服务的数据都是不同的。
import java.util.*;
import java.io.*;
public class Assignment2_main {
Services[] a; // a 是一个模板对象
int size;
static String name;
static String fileName;
static double usageFee;
static String toRemoveString = "service=";
public void ArrayList() {
a = (Services[]) new Object[10]; // a 定义为一个包含 10 个对象 T 的列表
size = 0;
}
public void add(Services item) { // 将项目添加到末尾
if (size == a.length) { // 如果大小等于 a 的长度
doubleArray(); // 数组扩展一倍
}
a[size++] = item; // 数组的最后一个对象取 item 的值
}
public void add(int pos, Services item) { // 将项目添加到特定位置
if (size == a.length) { // 如果大小等于 a 的长度
doubleArray(); // 数组扩展一倍
}
for (int i = size - 1; i > pos; i--) { // 指定位置之后的每个元素的索引都增加了一
a[i + 1] = a[i];
}
a[pos] = item; // 位置 pos 处的元素取 item 的值
size++; // 增加大小
}
public Services get(int pos) { // 获取位置 pos 处的值
if (pos >= size || pos < 0) { // 如果指定的位置越界,则显示错误
throw new IndexOutOfBoundsException();
}
return a[pos]; // 返回位置 pos 处的对象
}
public Services remove(int pos) { // 删除位置 pos 处的元素
if (pos >= size || pos < 0) { // 如果指定的位置越界,则显示错误
throw new IndexOutOfBoundsException();
}
Services temp = a[pos]; // 临时对象 T 取要删除的元素的值
for (int i = pos; i < size - 1; i++) { // 指定位置之后的每个元素的索引都减少了一
a[i] = a[i + 1];
}
size--; // 数组的大小减少了一
return temp; // 返回删除的值
}
public int size() { // 返回列表的大小
return size;
}
void doubleArray() {
Services[] newArray = (Services[]) new Object[a.length * 2]; // 将数组 a 的长度加倍
for (int i = 0; i < size; i++) { // 将数组 a 的所有元素放入数组 newArray
newArray[i] = a[i];
}
a = newArray; // 将所有元素放回 a
}
public static void main(String[] args) {
ArrayList<Services> differentServices = new ArrayList<Services>();
try {
File configFile = new File("/Users/Yohan/Desktop/CS245Assignment2/config.txt");
Scanner scanConfigFile = new Scanner(configFile);
while (scanConfigFile.hasNextLine()) {
String data = scanConfigFile.nextLine();
String[] token = data.split(",");
for (int i = 0; i < token.length; i++) {
if (i == 0) {
name = token[i].replaceAll("service=", "");
} else if (i == 1) {
fileName = token[i];
} else if (i == 2) {
usageFee = Double.parseDouble(token[i]);
} else {
System.out.println("Logical Error");
}
}
Services newService = new Services(name, fileName, usageFee);
// 在此之前有问题
differentServices.add(newService);
System.out.println(differentServices.get(0).fileName);
}
scanConfigFile.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
System.out.println(differentServices.get(0).fileName);
String userItemInput = "", userSizeInput = "", userQuantityInput = "";
Scanner input = new Scanner(System.in);
while (!(userItemInput.equals("done"))) {
System.out.println("Enter your next item or 'done':");
userItemInput = input.nextLine();
if (userItemInput.equals("done")) {
continue;
}
System.out.println("Size:");
userSizeInput = input.nextLine();
System.out.println("Quantity:");
userQuantityInput = input.nextLine();
}
}
}
这是配置文件:
service=Instacart,instacart.csv,5.99
service=Prime,amazon.csv,4.00
service=Safeway,safeway.csv,3.99
service=GoogleDelivery,google.csv,6.99
service=Basket,basket.csv,2.50
这是我的 Service.java 文件:
public class Services {
public String name = "";
public static String fileName = "";
public double usageFee = 0;
// 构造函数
public Services(String name, String fileName, double usageFee) {
this.name = name;
this.fileName = fileName;
this.usageFee = usageFee;
}
}
英文:
I am creating an Array List differentServices composed of objects Services I am creating.
When I'm trying to get data from a file which would serve as services with parameters service name, service file name, and service fee and while printing the values of my array list differentService I note that the name of the services in my arraylist are all different and according to the file, the service fees of the different objects in the arraylist are all different and according to the file too, but the filename of every object in differentServices is the same even though the way I declare the new Services() is the same as for service name and service fee.
I'm trying to understand why as I need the data of every service to be different.
import java.util.*;
import java.io.*;
public class Assignment2_main {
Services[] a; //a is a Template object
int size;
static String name;
static String fileName;
static double usageFee;
static String toRemoveString = "service=";
public void ArrayList() {
a = (Services[]) new Object[10]; //a defined as a list of 10 Objects T
size = 0;
}
public void add(Services item) { //Add item to the end
if(size == a.length) { //If the size is equal to the length of a
doubleArray(); //Double the array
}
a[size++] = item; //The last object of the array takes the value of item
}
public void add(int pos, Services item) { //Add item to specific position
if(size == a.length) {//Double the array if the size is equal to the length of a
doubleArray();
}
for(int i=size-1; i>pos; i--) { //Every element after the position specified is changing index by one
a[i+1] = a[i];
}
a[pos] = item; //The element at position pos takes the value of item
size++; //Increment the size
}
public Services get(int pos) { //Get value at position pos
if(pos >= size || pos < 0) { //If the specified position is out of bound, display error
throw new IndexOutOfBoundsException();
}
return a[pos]; //Return the object at position pos
}
public Services remove(int pos) { //Remove element at position pos
if(pos >= size || pos < 0) { //If the specified position is out of bound, display error
throw new IndexOutOfBoundsException();
}
Services temp = a[pos]; //temp object T takes value of the element to remove
for(int i=pos; i<size-1; i++) { //Every element after the position specified is changing index by one
a[i] = a[i+1];
}
size--; //size of the array decrements by one
return temp; //Return the removed value
}
public int size() { //Return the size of the list
return size;
}
void doubleArray() {
Services[] newArray = (Services[])new Object[a.length*2]; //Doubles the length of the array a
for(int i = 0; i < size; i++) { //places all the elements of the array a into array newArray
newArray[i] = a[i];
}
a = newArray; //places all the elements back into a
}
public static void main(String[] args) {
ArrayList<Services> differentServices = new ArrayList<Services>();
try {
File configFile = new File("/Users/Yohan/Desktop/CS245Assignment2/config.txt");
Scanner scanConfigFile = new Scanner(configFile);
while (scanConfigFile.hasNextLine()) {
String data = scanConfigFile.nextLine();
String[] token = data.split(",");
for(int i=0;i<token.length;i++) {
if(i==0) {
name = token[i].replaceAll("service=", "");
}
else if(i==1) {
fileName = token[i];
}
else if(i==2) {
usageFee = Double.parseDouble(token[i]);
}
else {
System.out.println("Logical Error");
}
}
Services newService = new Services(name, fileName, usageFee);
//Problem before this
differentServices.add(newService);
System.out.println(differentServices.get(0).fileName);
}
scanConfigFile.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
System.out.println(differentServices.get(0).fileName);
String userItemInput = "", userSizeInput = "", userQuantityInput = "";
Scanner input = new Scanner(System.in);
while(!(userItemInput.contentEquals("done"))) {
System.out.println("Enter your next item or 'done':");
userItemInput = input.nextLine();
if (userItemInput.contentEquals("done")) {
continue;
}
System.out.println("Size:");
userSizeInput = input.nextLine();
System.out.println("Quantity:");
userQuantityInput = input.nextLine();
}
}
}
Here is the configuration file:
service=Instacart,instacart.csv,5.99
service=Prime,amazon.csv,4.00
service=Safeway,safeway.csv,3.99
service=GoogleDelivery,google.csv,6.99
service=Basket,basket.csv,2.50
And here is my Service.java file:
public class Services {
public String name = "";
public static String fileName = "";
public double usageFee = 0;
//constructor
public Services(String name, String fileName, double usageFee) {
this.name = name;
this.fileName = fileName;
this.usageFee = usageFee;
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论