Tutorials for bugs and Java syntax

I ask for help about self-help, which is a type of oxymoron. How can I rid you of good people by solving more of my own problems?

I am in the last week of Java programming, and I have a huge obstacle in learning Java. I have read all the books, but I keep focusing on small issues all the time. It is like trying to build a house of cards. I only know about those parts of the syntax and uses that the book shows. When I combine things, I face terrible obstacles. I have been trying to understand them for many hours. Only basic uses are displayed in the sun, which do not seem to help.

Here is what I would like:

When I tried something and it doesnโ€™t work, like the following manipulations with the list of arrays, I want to find a place or program that can show an example of code for such things as adding an additional instance of the class to arrayList. Where can I briefly find out about this without asking a question or 2 for each syntax error? Where is Google for Java? Is there a program that will take your mistakes 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
+1
source share
9 answers

dvdlist ArrayList, Collection, (BTW, " , ", dvdlist java.util.List):

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

Collection, , .

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

add(Object o) dvdlist.

RatedDVD, remove(Object o) dvdlist.

search() contains(Object o) dvdlist.

, Iterator:

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

Java 5+ Generics:

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

, Framework .

+5

- , , , , . , , ArrayList. docs, , add() remove(), add() delete() . ArrayList, . ; , API. , .

+3

, :

/tmp/jc_4083/Inventory.java:101: incompatible types
found   : RatedDVD[]
required: java.util.ArrayList
        dvdlist = temp;

" " java.util.ArrayList, RatedDVD[].

, , Python, Java . . - , ArrayList - , .

, . , , , java.util.Arrays.asList() List.toArray().

, - Sun Java-, . .

+2

IDE (, Eclipse, ). API, , . , StackOverflow.

, , .

+1

, . , .

, , Java (PDF), , . , Java Glossary . .

+1

, , ArrayList, , , Java . list.get(i), i- . , ArrayList , . ArrayList temp, dvdlist, . dvdlist = new ArrayList<RatedDVD>(temp);.

: API Java, , , API Java. , ArrayList - , , . Java , List, . , , ArrayList .

0

, ArrayList. - []. ArrayList - Collections Java, , , , .. .

.. , . . , , - . , . (, ). . , , .. Google - , . , - - , , " " , , . - , 15 ( Borland Pascal - yuck).

0

, , , .

, , javac ( /). , , (.. , ) , .

0

- ...

, - ( , ) - , , , .

Java - , Java . Java , , , - . , , .

. , , , .

So, think of data structures as black boxes with methods, and then see how you do it yourself.

You may have been here, but this is what you can do with an ArrayList . And you have ArrayList<RatedDVD>one that further limits what you can do with it. Try to understand this first, and then fix the program to work with the available operations on the ArrayList object.

0
source

Source: https://habr.com/ru/post/1763990/


All Articles