本文介绍了Java错误和语法的教程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我要求自助的帮助,这是一种矛盾。我如何通过解决更多的自己的问题,减少你的好人? 我在上周的Java编程,我有一个巨大的障碍学习Java 。我读了所有的书,但我仍然挂在微小的小问题上。这就像试图建一个房子的卡。我只知道该书展示的语法和用法的部分。当我结合的东西,我遇到了可怕的障碍。我试着花几个小时来修理它们。 这里是我想要的: >当我尝试某些东西,它不工作,像下面的操作数组列表,我想找到一个地方或程序,可以显示的事情的示例代码,如添加一个额外的类实例到arrayList。在哪里可以简洁地学习这个,而不必问一个问题或2每个语法错误? Google for Java在哪里?是否有程序会接受您的错误并告诉您如何修复它们(或提供建议)? / tmp / jc_4083 /Inventory.java:101:不兼容类型发现:RatedDVD [] 必需:java.util.ArrayList dvdlist = temp; ^ /tmp/jc_4083/Inventory.java:110:array required,但是java.util.ArrayList找到 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:不兼容类型发现:RatedDVD [] 必需:java.util.ArrayList dvdlist = temp; 这是我的代码,如果任何人有兴趣为我看看: // Contruct inv和允许方法add,get,size,sort和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>(); } //结束方法 //添加& get public RatedDVD get(int i){return dvdlist.get(i);} //方法get public void add(DVD d){ dvdlist = dvdlist d ; sort(); } //方法add public double value() {//方法值 double total = 0.0; for(int i = 0; i< dvdlist.size(); i ++) {//每次传递到dvdlist添加总计 // [DEBUG] $ b total + = get(i).feeValue(); } return total; } //结束方法值 public void sort() {//方法排序 // [DEBUG]考虑优化 int n = dvdlist.size(); for(int search = 1; search< n; search ++) {//用于执行以下操作并递增到dvdlist被搜索 for(int i = 0; i {//如果需要交换则交换 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 } //结束方法sort public int size(){return dvdlist.size();} //方法大小 public void save(){ save(true); } //保存到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 RatedDVD dvd = get(i); w.write(dvd.getItem()+\\\); w.write(dvd.getName()+\\\); w.write(dvd.getRating()+\\\); w.write(dvd.getUnits()+\\\); w.write(dvd.getPrice()+\\\); w.write(dvd.value()+\\\); w.write(dvd.fee()+\\\); w.write(dvd.feeValue()+\\\); w.newLine(); } //它的总值 //w.write(value()+\\\); w.close(); } catch(Exception ex){ if(saveagain){ new File(c:\\data\\)。mkdir(); // make file if does not exist save(false); } } } public int search(String name){ for(int i = 0; i if(get(i).getName()。equalsIgnoreCase(name))return i; } return -1; //我们没有找到任何东西} //添加一个新的dvd到最后,增加数组大小 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; } //从数组中删除一个DVD,并缩小数组大小 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 [一世]; } else adj = 1; } dvdlist = temp; } public int highestNumber(){ int numb = 0; for(int i = 0; i if(get(i).getItem()> numb){ numb = get i).getItem(); } } return numb; } } //结束类库存 解决方案> dvdlist 是一个 ArrayList ,它实现了集合接口,而不是Array(BTW,这就是所谓的程序到接口,而不是实现原则,你应该decalare dvdlist 作为 java.util.List ): private ArrayList< RatedDVD> dvdlist; // declare dvdlist as ArrayList of RatedDVD 看一下Collection接口上的方法, 因此,要添加 RatedDVD ,您将不需要使用一个 RatedDVD 的临时数组,不会像你所做的那样适合 ArrayList 这里: //添加一个新的dvd到末端,增加数组大小 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;相反,只需调用 add(Object o)即可。} $ b dvdlist 删除 RatedDVD 实例,使用 dvdlist 上的 remove(Object o) 对于search()方法,请考虑在 dvdlist上使用 contains(Object o) 如果您需要遍历集合,请使用迭代器: for(Iterator iter = dvdlist.iterator(); iter.hasNext();){ RatedDVD ratedDVD =(RatedDVD)iter.next(); //删除了其余的代码块} 与Java 5+和泛型: for(RatedDVD ratingDVD:dvdlist){ //此处的其余代码} 真的,你需要挖掘集合框架。 I am asking for help on self-help, which is kind of an oxymoron. How do I bug you nice folks less by solving more of my own problems?I am in my last week of Java programming and I am having a huge hurdle with learning Java. I have read all the books but I keep getting hung up on tiny little issues. It is like trying to build a house of cards. I only know about the parts of the syntax and the uses that the book shows. When I am combining things, I run into horrible hurdles. I try for hours of tinkering to figure them out. The sun docs only show basic uses that don't seem to helpHere is what I would like: When I am trying something and it doesn't work like the following manipulations of an array list, I want to find a place or program that can show examples code of things like adding an additional class instance to an arrayList. Where can I learn concisely about this without having to ask a question or 2 for every syntax error? Where is the Google for Java? Is there a program that will take your errors and show you how to fix them (or offer suggestions)?/tmp/jc_4083/Inventory.java:101: incompatible typesfound : 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 typesfound : RatedDVD[]required: java.util.ArrayList dvdlist = temp;Here is my code for this class if anyone is interested in looking at it for me://Contruct inv and allow for methods add, get, size, sort, and valueimport 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 解决方案 The dvdlist is an ArrayList, which implements the Collection interface, not an Array (BTW, and this is known as the "program to an interface, not an implementation" principle, you should decalare dvdlist as a java.util.List):private ArrayList<RatedDVD> dvdlist;// declare dvdlist as ArrayList of RatedDVDHave a look at the methods on the Collection interface, you'll find everything you need for adding and removing elements.So, to add a RatedDVD, you don't need to use a temporary array of RatedDVD that won't fit anyway into an ArrayList like you're doing here:// add a new dvd to the end, increasing the array sizepublic 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;}Instead, just call the add(Object o) method on dvdlist.To delete a RatedDVD instance, use the remove(Object o) method on dvdlist. For the search() method, consider using contains(Object o) on dvdlist.If you need to iterate over a collection, use an Iterator:for (Iterator iter = dvdlist.iterator(); iter.hasNext();) { RatedDVD ratedDVD = (RatedDVD) iter.next(); //rest of the code block removed}Or even faster now with Java 5+ and Generics:for (RatedDVD ratedDVD : dvdlist) { // rest of the code here}Really, you need to dig the the Collection Framework. 这篇关于Java错误和语法的教程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-19 07:35