教程面向Java错误和语法 [英] Tutorials for Java errors and syntax

查看:114
本文介绍了教程面向Java错误和语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要求自救,这是一种自相矛盾的帮助。我怎么打扰你漂亮的人较少受到更多解决我自己的问题?

我在我的最后一周Java编程和我有学习Java的一个巨大的障碍。我读所有的书,但我不断收到挂了小小的问题。这就像试图建立纸牌做的房子。我只知道有关语法的零部件和这本书展示了用途。当我合并的事,我碰到可怕的障碍。我尝试修修补补的小时图出来。太阳文档只能说明似乎并没有帮助基本用法

下面是我想什么:

当我想的东西,它并不像一个数组列表下面的操作工作,我想找个地方或程序,可以显示例子的事情code像增加一个额外的类实例到一个ArrayList 。我在哪里可以简明这个学习,而不必问每一个语法错误的问题或2?哪里是谷歌的Java?是否有一个程序,将你的错误,并告诉你如何解决这些问题(或提供建议)?

  /tmp/jc_4083/Inventory.java:101:不兼容的类型
发现:RatedDVD []
要求:java.util.ArrayList中
    dvdlist =温度;
    ^
/tmp/jc_4083/Inventory.java:110:阵列必需的,但发现的java.util.ArrayList
    如果(对!= dvdlist [I]){
    ^
/tmp/jc_4083/Inventory.java:111:阵列必需的,但发现的java.util.ArrayList
    温度[I-ADJ] = dvdlist [I]
    ^
/tmp/jc_4083/Inventory.java:115:不兼容的类型
发现:RatedDVD []
要求:java.util.ArrayList中
    dvdlist =温度;

下面是我的code表示如果有人有兴趣在这找我这个类:

  // Contruct INV,并允许添加的方法,得到的,大小,排序和价值
进口的java.util。*;
进口java.io.BufferedWriter中;
进口的java.io.File;
进口java.io.FileWriter;公共类库存
{//类库存
    私人的ArrayList< RatedDVD> dvdlist; //声明dvdlist作为RatedDVD的ArrayList的
    私人诠释numDVDs;    公共库存()
    {//方法库存
    dvdlist =新的ArrayList< RatedDVD>();    } // end方法    //添加&安培;得到
    公共RatedDVD的get(int i)以{返回dvdlist.get(我);} //获取方法    公共无效添加(DVD D){
    dvdlist = dvdlist D组;
    分类();
    } //添加方法    公共双值()
    {//方法的价值
    双总= 0.0;
    的for(int i = 0; I< dvdlist.size();我++)
    {//每经过直通dvdlist加总
    // [DEBUG]考虑增强
    总+ = GET(我).feeValue();
    }
    总回报;
    } // end方法的价值    公共无效的sort()
    {//方法排序
    // [DEBUG]考虑优化
    INT N = dvdlist.size();
    对于(INT搜索= 1;搜索和LT; N;搜索++)
    {//用于执行以下操作和增量直到dvdlist已被搜查
    的for(int i = 0; I< N-搜索;我++)
    {//通过对整个dvdlist比较步骤
    如果(dvdlist.get(ⅰ).getName()与compareToIgnoreCase(dvdlist.get第(i + 1).getName())大于0)
    {//如果有必要交换然后交换
    RatedDVD TEMP = dvdlist.get(I)
    dvdlist.set(ⅰ,dvdlist.get第(i + 1));
    dvdlist.set第(i + 1,温度);
    } // end如果交换
    } //末尾的compareTo
    } //结束的外
    } // end方法排序    公众诠释大小(){返回dvdlist.size();} //方法大小    公共无效保存(){
    保存(真);
    }    //保存到C:\\ DATA \\ inventory.dat
    公共无效保存(布尔saveagain){
    尝试{
    BufferedWriter将W =新的BufferedWriter(新FileWriter的(C:\\\\ \\\\数据inventory.dat));
    的for(int i = 0; I<尺寸();我++){
    RatedDVD DVD =得到(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();
    }
    //它的总价值
    //w.write(值()+\\ n);
    w.close();
    }赶上(例外前){
    如果(saveagain){
    新的文件(C:\\\\数据\\\\)的mkdir(); //使文件,如果不存在
    保存(假);
    }
    }
    }    公众诠释搜索(字符串名称){
    的for(int i = 0; I<尺寸();我++){//检查名称的字符串相等
    如果(获取(I).getName()equalsIgnoreCase(名)。)回报我;
    }
    返回-1; //我们没发现什么
    }    //添加新的DVD到结束时,增加数组的大小
    公共无效添加(RatedDVD P){
    RatedDVD [] TEMP =新RatedDVD [dvdlist.size()+ 1];
    的for(int i = 0; I< dvdlist.size();我++){
    临时[I] ​​= dvdlist [I]
    }
    温度[temp.length-1] = P; //在末尾添加
    dvdlist =温度;
    }    //从数组中删除一个DVD和缩小数组的大小
    公共无效删除(RatedDVD P){
    RatedDVD [] TEMP =新RatedDVD [dvdlist.size() - 1];
    INT ADJ = 0;
    的for(int i = 0; I< dvdlist.size();我++){
    如果(对!= dvdlist [I]){
    温度[I-ADJ] = dvdlist [I]
    }
    别的形容词= 1;
    }
    dvdlist =温度;
    }
    公众诠释highestNumber(){
    INT麻木= 0;
    的for(int i = 0; I< dvdlist.size();我++){
    如果(获取(I).getItem()>麻木){
    麻木= GET(我).getItem();
    }
    }
    返回麻木;
    }
} //结束类盘点


解决方案

dvdlist 是的 ArrayList的,它实现的收藏接口,而不是一个数组(顺便说一句,这是被称为程序的接口,而不是实现原则上,你应该decalare dvdlist 的java.util.List

 私人的ArrayList< RatedDVD> dvdlist; //声明dvdlist作为RatedDVD的ArrayList的

看一看Collection接口的方法,你会发现你需要添加和删除元素应有尽有。

所以,添加 RatedDVD ,你并不需要使用 RatedDVD 赢得的临时数组'吨适合反正成的ArrayList 就像你在这里做什么:

  //添加一个新的DVD到最后,增加数组的大小
公共无效添加(RatedDVD P){
    RatedDVD [] TEMP =新RatedDVD [dvdlist.size()+ 1];
    的for(int i = 0; I< dvdlist.size();我++){
            临时[I] ​​= dvdlist [I]
    }
    温度[temp.length-1] = P; //在末尾添加
    dvdlist =温度;
}

相反,只需调用添加(对象o)方法上的 dvdlist

要删除 RatedDVD 例如,使用删除(对象o)方法dvdlist

有关搜索()方法,可以考虑使用包含(对象o) dvdlist

如果您需要遍历集合,使用的迭代器的:

 的(迭代器ITER = dvdlist.iterator(); iter.hasNext();){
   RatedDVD ratedDVD =(RatedDVD)iter.next();
   //将code座的其余部分去除
}

甚至更快了与Java 5+和泛型:

 为(RatedDVD ratedDVD:dvdlist){
   //将code的其余部分在这里
}

真的,你需要挖的收集框架

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 help

Here 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 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;

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 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

解决方案

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 RatedDVD

Have 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 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;
}

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错误和语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆