Anonymous
Anonymous asked in Computers & InternetProgramming & Design · 7 months ago

Java Error: array required, but java.util.ArrayList<Desktop> found?

I'm trying to make an electronic store that sells desktops, laptops, and fridges. As part of the ElectronicStore class, it should have a void method called printStock() that will iterate over all of the store’s stock and print them to the console in a readable format. When I try to create this method, I keep getting the error: Error: array required, but java.util.ArrayList<Desktop> found.

What I tried to do below is get each element in the arraylist desktops and such, and output them.

public void printStock(){

System.out.println("This store's stock includes:");

for(int i = 0; i <= desktops.size(); i++){

System.out.println(desktops[i]);

}

for(int i = 0; i <= laptops.size(); i++){

System.out.println(laptops[i]);

}

for(int i = 0; i <= fridges.size(); i++){

System.out.println(fridges[i]);

}

}

It should print something like:

The store stock includes:

Desktop PC with 3.5ghz CPU, 8GB RAM, 500GB HDD drive.

Desktop PC with 3.0ghz CPU, 16GB RAM, 250GB SSD drive.

Desktop PC with 4.3ghz CPU, 32GB RAM, 500GB SSD drive.

15" Laptop PC with 3.1ghz CPU, 32GB RAM, 500GB SSD drive.

13" Laptop PC with 2.5ghz CPU, 8GB RAM, 250GB HDD drive.

15" Laptop PC with 3.0ghz CPU, 16GB RAM, 250GB SSD drive.

16.5 cu. ft. Fridge with Freezer (Black)

12.0 cu. ft. Fridge (White)

23.0 cu. ft. Fridge with Freezer (Stainless Steel)

Update:

anyone know how to fix this?

2 Answers

Relevance
  • 7 months ago
    Best Answer

    Java doesn't allow overriding operators, so ArrayList objects can't use [] to access elements by index.

    Instead of desktops[i], use desktops.get(i) to get the value at index i, and use desktops.set(i, newValue) to store a new value at index i.

    Do the same with laptops and/or fridges since they seem to be ArrayList objects, too.

    Another option is to change your for loop to an "enhanced for" loop (also called a "for each" loop).

    for (Desktop dt : desktops) {

    .... System.out.println(dt);

    }

    That runs once for each element of the desktops list. On each iteration of the loop, the loop variable dt holds a reference to the current element of the list. The loop above, simply prints out each entry, just as your index-based loop does. That loop is simpler and typically faster than a regular for loop to do the same thing, but is only useful when you want to loop over all elements in order, and when you don't need the value of the index for anything inside the loop.

  • John
    Lv 7
    7 months ago

    sample

    import java.util.*;

    public class Program {

    public static void main(String[] args) {

    List<Desktop> desktops = new ArrayList<>();

    desktops.add(new Desktop("Desktop PC with 3.5ghz CPU, 8GB RAM, 500GB HDD drive"));

    desktops.add(new Desktop("Desktop PC with 3.0ghz CPU, 16GB RAM, 250GB SSD drive"));

    for (Desktop desktop : desktops) {

    System.out.println(desktop);

    }

    }

    }

    class Desktop {

    private String description;

    public Desktop(String description) {

    this.description = description;

    }

    @Override

    public String toString() {

    return this.description;

    }

    }

Still have questions? Get your answers by asking now.