gpt4 book ai didi

java - Java 错误和语法教程

转载 作者:行者123 更新时间:2023-11-29 09:46:37 25 4
gpt4 key购买 nike

我正在寻求有关自助的帮助,这有点矛盾。我如何通过解决更多我自己的问题来减少对你们好人的骚扰?

我在 Java 编程的最后一周,我在学习 Java 方面遇到了巨大的障碍。我读过所有的书,但我总是被一些小问题困扰。这就像尝试 build 纸牌屋。我只知道书中显示的语法部分和用法。当我组合事物时,我会遇到可怕的障碍。我尝试了数小时的修补来弄清楚它们。 sun 文档仅显示似乎没有帮助的基本用法

这是我想要的:

当我尝试做某事但它不像下面的数组列表操作那样工作时,我想找到一个地方或程序来显示示例代码,例如向 arrayList 添加一个额外的类实例。我在哪里可以简明地了解这一点,而不必为每个语法错误问一个或两个问题? Google for Java 在哪里?是否有程序可以接受您的错误并向您展示如何修复它们(或提供建议)?

/tmp/jc_4083/Inventory.java:101: incompatible types
found : RatedDVD[]
required: java.util.ArrayList
dvdlist = temp;
^
/tmp/jc_4083/Inventory.java:110: array required, but java.util.ArrayList found
if (p != dvdlist[i]) {
^
/tmp/jc_4083/Inventory.java:111: array required, but java.util.ArrayList found
temp[i-adj] = dvdlist[i];
^
/tmp/jc_4083/Inventory.java:115: incompatible types
found : RatedDVD[]
required: java.util.ArrayList
dvdlist = temp;

如果有人有兴趣帮我看一下,这是我为这门课编写的代码:

//Contruct inv and allow for methods add, get, size, sort, and value
import java.util.*;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;

public class Inventory
{// class Inventory
private ArrayList<RatedDVD> dvdlist;// declare dvdlist as ArrayList of RatedDVD
private int numDVDs;

public Inventory()
{// method Inventory
dvdlist = new ArrayList<RatedDVD>();

}// end method

// add & get
public RatedDVD get(int i){return dvdlist.get(i);}// method get

public void add(DVD d){
dvdlist = dvdlist d;
sort();
}// method add

public double value()
{// method value
double total = 0.0;
for (int i = 0; i < dvdlist.size(); i++)
{// for every pass thru dvdlist add total
// [DEBUG] consider enhanced for
total += get(i).feeValue();
}
return total;
}// end method value

public void sort()
{// method sort
// [DEBUG] consider optimization
int n = dvdlist.size();
for (int search = 1; search < n; search++)
{// for do the following and increment till dvdlist has been searched
for (int i = 0; i < n-search; i++)
{// for step through comparison for entire dvdlist
if (dvdlist.get(i).getName().compareToIgnoreCase(dvdlist.get(i+1).getName()) > 0)
{// if swap necessary then swap
RatedDVD temp = dvdlist.get(i);
dvdlist.set(i,dvdlist.get(i+1));
dvdlist.set(i+1,temp);
}// end if swap
}// end for compareto
}// end outer for
}// end method sort

public int size(){return dvdlist.size();}// method size

public void save() {
save(true);
}

// save it to C:\data\inventory.dat
public void save(boolean saveagain) {
try {
BufferedWriter w = new BufferedWriter(new FileWriter("c:\\data\\inventory.dat"));
for (int i = 0; i < size(); i++) {
RatedDVD dvd = get(i);
w.write( dvd.getItem() + "\n");
w.write( dvd.getName() + "\n");
w.write( dvd.getRating() + "\n");
w.write( dvd.getUnits() + "\n");
w.write( dvd.getPrice() + "\n");
w.write( dvd.value() + "\n");
w.write( dvd.fee() + "\n");
w.write( dvd.feeValue() + "\n");
w.newLine();
}
// total value of it
//w.write( value() + "\n");
w.close();
} catch (Exception ex) {
if (saveagain) {
new File("c:\\data\\").mkdir(); // make file if doesn't exist
save(false);
}
}
}

public int search(String name) {
for (int i = 0; i < size(); i++) { // check if name string is equal
if (get(i).getName().equalsIgnoreCase(name)) return i;
}
return -1; // we didn't find anything
}

// add a new dvd to the end, increasing the array size
public void add(RatedDVD p) {
RatedDVD[] temp = new RatedDVD[dvdlist.size()+1];
for (int i = 0; i < dvdlist.size(); i++) {
temp[i] = dvdlist[i];
}
temp[temp.length-1] = p; // add it at the end
dvdlist = temp;
}

// remove a DVD from the array, and shrink the array size
public void delete(RatedDVD p) {
RatedDVD[] temp = new RatedDVD[dvdlist.size()-1];
int adj = 0;
for (int i = 0; i < dvdlist.size(); i++) {
if (p != dvdlist[i]) {
temp[i-adj] = dvdlist[i];
}
else adj = 1;
}
dvdlist = temp;
}
public int highestNumber() {
int numb = 0;
for (int i = 0; i < dvdlist.size(); i++) {
if (get(i).getItem() > numb) {
numb = get(i).getItem();
}
}
return numb;
}
}// end class inventory

最佳答案

dvdlist 是一个 ArrayList , 它实现了 Collection接口(interface),而不是数组(顺便说一句,这被称为“接口(interface)程序,而不是实现”原则,您应该将 dvdlist 声明为 java.util.List):

private ArrayList<RatedDVD> dvdlist;// declare dvdlist as ArrayList of RatedDVD

看看 Collection 接口(interface)上的方法,您会发现添加和删除元素所需的一切。

因此,要添加 RatedDVD,您不需要使用 RatedDVD 的临时数组,它无论如何都无法放入 ArrayList 就像你在这里做的那样:

// add a new dvd to the end, increasing the array size
public void add(RatedDVD p) {
RatedDVD[] temp = new RatedDVD[dvdlist.size()+1];
for (int i = 0; i < dvdlist.size(); i++) {
temp[i] = dvdlist[i];
}
temp[temp.length-1] = p; // add it at the end
dvdlist = temp;
}

相反,只需在 dvdlist 上调用 add(Object o) 方法即可。

要删除 RatedDVD 实例,请使用 dvdlist 上的 remove(Object o) 方法。

对于 search() 方法,考虑在 dvdlist 上使用 contains(Object o)

如果您需要遍历集合,请使用 Iterator :

for (Iterator iter = dvdlist.iterator(); iter.hasNext();) {
RatedDVD ratedDVD = (RatedDVD) iter.next();
//rest of the code block removed
}

或者现在使用 Java 5+ 和泛型甚至更快:

for (RatedDVD ratedDVD : dvdlist) {
// rest of the code here
}

真的,你需要挖掘 Collection Framework .

关于java - Java 错误和语法教程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1402470/

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