如何将信息添加到对象类?

时间:2022-04-12 07:35:20
class Client{
String name;

Client(String name){
this.name=name;
}

Client (){
name=null;
}
}

class Cashier{

LinkedList <Client> cashierclients;


Cashier(LinkedList<Client> cashierclients){
this.cashierclients=cashierclients;
}
Cashier(){
//this.cashierclients=null;
}

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

LinkedList<Client> l=new LinkedList<Client>();

//i do some scans here to add the names of persons to the LinkedList l                   

Cashier [] cashier=new Cashier[numberofcashiers];

for(int i=0;i<numberofcashiers; i++){
    cashier[i]=new Cashier();
}

Now I want to add a client to cashier[0] i do this:

现在我想将一个客户端添加到收银员[0]我这样做:

cashier[0].cashierclients.addLast(l.get(0));

everything works fine but when I want to print the size of cashier[1] it returns 1 but it should return 0 because I did not add any person to cashier[1]. Any ideas as to what is wrong?

一切正常,但当我想打印收银员[1]的大小时,它返回1但它应该返回0因为我没有向收银员添加任何人[1]。关于什么是错的任何想法?

// the error happens here
cashier[1].cashierclients.size();

2 个解决方案

#1


the program is working now and it shows the error in cashier[1] size that should return 0 instead of 1

该程序现在正在工作,它显示收银员[1]大小的错误,应该返回0而不是1

import java.util.*;
class Client {
String name;

Client(String name) {
    this.name = name;
}

Client() {
    name = null;
}
}

class Cashier {

LinkedList<Client> cashierclients;


Cashier(LinkedList<Client> cashierclients) {
    this.cashierclients = cashierclients;
}

Cashier() {
//this.cashierclients=null;
}

}

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

    LinkedList<Client> l = new LinkedList<Client>();

// i created this LinkedList to solve the  nullpointerexception error

LinkedList<Client> empty =new LinkedList<Client>();

Client client1=new Client("Person1");
l.addLast(client1);

    Cashier[] cashier = new Cashier[10];

    for (int i = 0; i < 10; i++) {
        cashier[i] = new Cashier();
    }

// i did this to solve the  nullpointerexception error

for(int i=0; i<10; i++){
    cashier[i].cashierclients=empty;
}

cashier[0].cashierclients.addLast(l.get(0));

System.out.println(cashier[1].cashierclients.size());
//this should return zero
System.out.println(cashier[1].cashierclients.size());

}
}

#2


The following code

以下代码

        for(int i=0; i<10; i++){
            cashier[i].cashierclients=empty;
        }

makes cashier[i].cashierclients point to the same object

使收银员[i] .cashierclients指向同一个对象

so you can find

所以你可以找到

System.out.println(cashier[0].cashierclients == cashier[1].cashierclients);

equals to true

等于真

i think you shall change empty to new LinkedList()

我认为你应该将空更改为新的LinkedList()

that will fix the probelm

这将解决问题

#1


the program is working now and it shows the error in cashier[1] size that should return 0 instead of 1

该程序现在正在工作,它显示收银员[1]大小的错误,应该返回0而不是1

import java.util.*;
class Client {
String name;

Client(String name) {
    this.name = name;
}

Client() {
    name = null;
}
}

class Cashier {

LinkedList<Client> cashierclients;


Cashier(LinkedList<Client> cashierclients) {
    this.cashierclients = cashierclients;
}

Cashier() {
//this.cashierclients=null;
}

}

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

    LinkedList<Client> l = new LinkedList<Client>();

// i created this LinkedList to solve the  nullpointerexception error

LinkedList<Client> empty =new LinkedList<Client>();

Client client1=new Client("Person1");
l.addLast(client1);

    Cashier[] cashier = new Cashier[10];

    for (int i = 0; i < 10; i++) {
        cashier[i] = new Cashier();
    }

// i did this to solve the  nullpointerexception error

for(int i=0; i<10; i++){
    cashier[i].cashierclients=empty;
}

cashier[0].cashierclients.addLast(l.get(0));

System.out.println(cashier[1].cashierclients.size());
//this should return zero
System.out.println(cashier[1].cashierclients.size());

}
}

#2


The following code

以下代码

        for(int i=0; i<10; i++){
            cashier[i].cashierclients=empty;
        }

makes cashier[i].cashierclients point to the same object

使收银员[i] .cashierclients指向同一个对象

so you can find

所以你可以找到

System.out.println(cashier[0].cashierclients == cashier[1].cashierclients);

equals to true

等于真

i think you shall change empty to new LinkedList()

我认为你应该将空更改为新的LinkedList()

that will fix the probelm

这将解决问题