gpt4 book ai didi

java - 无法实例化数组列表类型

转载 作者:行者123 更新时间:2023-12-01 10:33:39 25 4
gpt4 key购买 nike

我对 Java 还很陌生,需要快速回答,所以不要判断我是否搜索得不够。所以我有 Antena 类并尝试创建 Controller 类,但我无法创建数组列表实例。代码未完成。有人可以解决这个问题吗? :D这是 Controller 类:

package paket;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import java.util.Scanner;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;

public class KontrolaAntene implements Initializable {
private List<Antena> antene= new ArrayList<Antena>();
private int broj;

@FXML
private Button btnIspisi;
@FXML
private Button btnIzlaz;
@FXML
private TextArea taIspis;
@FXML
private TextArea taIspis_izlaz;
@FXML
private Label label1;
@FXML
private Label label2;
@FXML
private Label label3;
@FXML
private TextField tfx;
@FXML
private TextField tfy;

@FXML
void ispisi(ActionEvent event){
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Izaberi datoteku");
fileChooser.getExtensionFilters().addAll(
new ExtensionFilter("Text files","*.txt"),
new ExtensionFilter("All files","*.txt")
);

String pocetakputanja = System.getProperty("user.dir");
File pocetak = new File(pocetakputanja);

if(!pocetak.canRead())
{
pocetak = new File("C:/");
}

fileChooser.setInitialDirectory(pocetak);

File selectedFile = fileChooser.showOpenDialog(TestAntene.mainStage);

if(selectedFile == null || selectedFile.getName()!="antene.txt"){
taIspis.setText("Datoteka antene.txt nije izabrana");
}
else{
taIspis.setText("");
ispisiFajl(selectedFile);
}


}

void ispisiFajl(File selectedFile){
antene.clear();
Path put = Paths.get(selectedFile.getPath());
Charset kod = StandardCharsets.UTF_8;
Scanner sc;
try {
sc = new Scanner(put, kod.name());
while(sc.hasNextLine()){
String linija = sc.nextLine();
String tip = linija.substring(0,linija.indexOf(":")).trim();
String delovi[]= linija.substring(linija.indexOf(":") + 1,
linija.length()).split(", ");
if(tip == "K"){
//THIS LINE IS A PROBLEM!
Antena nova = new Antena(Integer.parseInt(delovi[1].trim()),
Integer.parseInt(delovi[1].trim()),Integer.parseInt(delovi[1].trim()));
}
}


} catch (Exception e) {
// TODO Auto-generated catch block
taIspis.setText("Datoteka antene.txt nije izabrana");
}
}

@FXML
void ispisi_izlaz(ActionEvent event){
taIspis_izlaz.appendText("Broj antena koje pokrivaju zadato mesto je "+broj);
}

@Override
public void initialize(URL arg0, ResourceBundle arg1) {
// TODO Auto-generated method stub

}

}

这是 Antena 类:

打包;

公共(public)抽象类 Antena {

public int x;
public int y;
public int Domet1;
public int Domet2;

public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getDomet1() {
return Domet1;
}
public void setDomet1(int domet1) {
Domet1 = domet1;
}
public int getDomet2() {
return Domet2;
}
public void setDomet2(int domet2) {
Domet2 = domet2;
}
public Antena(int x, int y, int domet1, int domet2) {
super();
this.x = x;
this.y = y;
Domet1 = domet1;
Domet2 = domet2;
}

boolean uDometu(int x, int y)
{

double rastojanje;
int x2 = getX();
int y2 = getY();
int domet1= getDomet1();
int domet2= getDomet2();
if(getDomet2() == 0){
rastojanje= Math.sqrt((y-y2)*(y-y2)+(x-x2)*(x-x2));
if((double)getDomet1() >= rastojanje)
return true;
else
return false;
}
else
{
if((x2==x && y2+domet2>y)||
(x2==x && y2-domet2<y)||
(x2+domet1>x && y2==y)||
(x2-domet1<x && y2==y)){ return true;}
else if((x2+domet1>=x && y2+domet1>=y)||
(x2-domet1<=x && y2+domet1<=y)||
(x2+domet1<=x && y2+domet1>=y)||
(x2+domet1>=x && y2+domet1<=y))
{return true;}
else return false;
}
}

}

最佳答案

您似乎没有正确比较String,并且无法实例化abstract类。选项1,

  1. Antenna 中删除 abstract,或
  2. 创建一个具体子类并实例化该子类

类似的东西

if (tip.equals("K")) { // == "K"){
Antenna nova = new Antenna(Integer.parseInt(delovi[1].trim()), // ...

记住然后更改“Antena”,例如

public class Antenna { // <-- Not abstract

1我还更正了天线的拼写。

关于java - 无法实例化数组列表类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34937836/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com