[英]Is there a way to implement generics to have an ArrayList take in a boolean?
I have the following code, but the ArrayList is not taking in 'Date' type.我有以下代码,但 ArrayList 没有采用“日期”类型。 The only thing I can think of is to incorporate generics. However, I am running into problems incorporating it into the arraylist. I did some search online and have not been able to find a good source for it.我唯一能想到的就是合并 generics。但是,我在将它合并到 arraylist 时遇到了问题。我在网上做了一些搜索,但没能找到一个好的来源。
Main:主要的:
public class Main {
public static void main(String[] args) {
while(true)
{
ATM atm = new ATM();
atm.init();
atm.run();
}
}
}
class: class:
import java.util.Scanner;
class ATM{
private LCD lcd;
private CardReader cardReader;
private Keypad keypad;
private CashDispenser cashDispenser;
public void init(){
System.out.println("ATM.init()");
lcd = new LCD();
lcd.init();
cardReader = new CardReader();
cardReader.init();
cashDispenser = new CashDispenser();
cashDispenser.init();
}
public void run(){
int count = 0;
boolean correct;
int tries = 0;
System.out.println("ATM.run()");
System.out.println("Please select a four digit pin");
Scanner pin = new Scanner(System.in);
int pins = pin.nextInt();
int pins2 = pins;
keypad = new Keypad();
while(pins != pins2 && tries <= 4)
{
pins2 = keypad.init();
count++;
tries++;
}
if(pins == pins2)
{
correct = false;
}
else
{
correct = true;
}
String BANK_NAME = "NewCo";
System.out.println("ATM Version 0.1.0");
System.out.println("Copyright (C) 2020" + BANK_NAME);
System.out.println("Code by Julian Blanco");
User user = new User(lcd, cardReader,keypad, cashDispenser, count, correct);
while(true){
user.io();
}
}
}
class: class:
import java.util.Date;
import java.util.ArrayList;
public class UserInfo<T>
{
private ArrayList<T> values;
public UserInfo(){
this.values = new ArrayList<T>();
}
public void setValue(Date date,int count, boolean correct)
{
this.values.add(date, count, correct);
}
}
Class: Class:
import java.util.ArrayList;
import java.util.Date;
public class Logger
{
ArrayList<UserInfo> obj = new ArrayList<UserInfo>();
public Logger(Date date, int count, boolean correct){
obj.get(0).setValue(date,count, correct);
}
}
Error:错误:
java: incompatible types: java.util.Date cannot be converted to T
You cannot add Data the the List of T generic type.您不能将数据添加到 T 通用类型的列表中。 There is a problem in the setValue method. setValue 方法有问题。
public class UserInfo<T>
{
private ArrayList<T> values;
public UserInfo(){
this.values = new ArrayList<T>();
}
public void setValue(T value)
{
this.values.add(value);
}
}
Moreover, is it really necessary to use generics in the UserInfo class?而且,UserInfo class中真的有必要使用generics吗? If you want to store the date, count and correct values you should create a class for it.如果你想存储日期、计数和更正值,你应该为它创建一个 class。
In addition, I would get the depdendencies of the ATM object (lcd,...) in a Constructor and only initailize them in the init method.此外,我将在构造函数中获取 ATM object(lcd,...)的依赖项,并且仅在 init 方法中初始化它们。 It facilitate the testing of your code.它有助于测试您的代码。
class ATM{
private LCD lcd;
private CardReader cardReader;
private Keypad keypad;
private CashDispenser cashDispenser;
public ATM(LCD lcd, CardReader cardReader, Keypad keypad, CashDispenser cashDispenser){
this.lcd = lcd;
this.cardReader = cardReader;
this.keypad = keypad;
this.cashDispenser = cashDispenser;
}
public void init(){
System.out.println("ATM.init()");
lcd.init();
cardReader.init();
cashDispenser.init();
}
I guess what you really would like to do is use UserInfo as a class to hold your data and save that data in a generic Java List.我猜你真正想做的是将 UserInfo 用作 class 来保存你的数据并将该数据保存在通用 Java 列表中。
So define UserInfo like this:所以像这样定义 UserInfo:
public class UserInfo {
private final Date date;
private final int count;
private final boolean correct;
public UserInfo(Date date, int count, boolean correct) {
this.date = date;
this.count = count;
this.correct = correct;
}
public Date getDate() {
return date;
}
public int getCount() {
return count;
}
public boolean isCorrect() {
return correct;
}
}
And then you can use the normal List.add like this:然后你可以像这样使用普通的 List.add :
List<UserInfo> userInfo = new ArrayList<>();
userInfo.add(new UserInfo(someDate, 1, true));
First thing we need to note is that your class UserInfo
is a generic class我们需要注意的第一件事是您的 class UserInfo
是通用的 class
The second thing is that the ArrayList<T>
is also a generic class;第二件事是ArrayList<T>
也是一个泛型 class; however, the generic type specified is whatever type is specified as the same as the type specified in for UserInfo
但是,指定的通用类型是指定的任何类型,与UserInfo
中指定的类型相同
So if we do UserInfo<String> info = new UserInfo<String>();
所以如果我们做UserInfo<String> info = new UserInfo<String>();
the ArrayList<T>
would effectively be ArrayList<String>
. ArrayList<T>
实际上是ArrayList<String>
。
In order to resolve your problem you should make your UserInfo
something like为了解决您的问题,您应该使您的UserInfo
类似于
import java.util.Date;
import java.util.ArrayList;
public class UserInfo {
private ArrayList<Date> values;
public UserInfo(){
this.values = new ArrayList<Date>();
}
public void setValue(Date date, int count, boolean correct){
this.values.add(date, count, correct);
}
}
Although you didn't ask about this, the I'm fairly certain that ArrayList does not have an overload for add that takes 3 arguments.虽然你没有问过这个问题,但我相当确定 ArrayList 没有一个需要 3 arguments 的加法过载。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.