Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6698317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:33:49+00:00 2026-05-26T06:33:49+00:00

this is my programm : public class Basket { private Item[] shops = {

  • 0

this is my programm :

public class Basket {
private Item[] shops = { new Item("1",1)} ; 

public void storeItems(Item it){

        if (arraysIndex > shops.length){
            resizeArray(shops);

        }
        *shops[arraysIndex++] = {it};
        *shops[arraysIndex++] = {new Item(it.getName(),it.getPrice())};

    }

    public <T> T[] resizeArray(T[] arrayToResize){

        int newCapacity = arrayToResize.length *2;
        T[] newArray = (T[]) Array.newInstance(arrayToResize[0].getClass(), newCapacity);
        System.arraycopy(arrayToResize, 0, newArray, 0, arrayToResize.length);

        return newArray;
    }

}

in the lines the I indicated with * I will get such this error :

“Array constants can only be used in initializers”

which I don’t know how to solve the problem in java please advice me about it.

regards

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:33:50+00:00Added an answer on May 26, 2026 at 6:33 am

    Simply lose the curly braces:

        shops[arraysIndex++] = it;
        shops[arraysIndex++] = new Item(it.getName(),it.getPrice());
    

    Also, there’s a bug here:

        if (arraysIndex > shops.length){
            resizeArray(shops);
        }
    

    Since array indices in Java start from zero, the correct comparison is if (arraysIndex >= shops.length).

    Also, if you’re using Java 1.6+, resizeArray() could be based on Arrays.copyOf().

    And, finally, you appear to be doing pretty much what ArrayList<T> does — why not simply use the latter and not worry about reallocations etc?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote this program in Java public class Why { public static void test()
public class Ex { public void eat(Animal animal){System.out.println(this is animal);} public void eat(Dog dog){System.out.println(this
Consider this programs public class ItemManager { private ItemFetcher itemFetcher; public ItemManager(ItemFetcher _itemFetcher) {
I have this class: public class Test { public static void main(String[] args) {
Why does this test program result in a java.lang.IllegalMonitorStateException ? public class test {
I have a program that looks something like this: public partial class It {
My program: public class m { public static void main (String[] args) { boolean
What is bad on this code? It is simple example: class Program { public
Why is this code not working? public class A { public Dictionary<int, string> dic
I came across this program and its not behaving in expected way. public class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.