1

i am building a program like a Shop that have products and have the options of sell them with price and everything .. i wanted to make a options that will allow me to add new Products to the array from the program the its self for doing that i need to increase my array length . and i kind of stuck know , ty for all your help !!

Proudact MilkProd = new Proudact("Milk", 5.55, 50, 10, 0);
Proudact ChessProd = new Proudact("Chess", 4.35, 50, 10, 1);
Proudact AppleProd = new Proudact("Apple", 1.25, 50, 10, 2);
Proudact WotermelonProd = new Proudact("Wotermelon", 6.21, 50, 10, 3);

Proudact [] MyArray = {MilkProd,ChessProd,AppleProd,WotermelonProd};
NewProd(MyArray);

public static void NewProd(Proudact[] MyArray){
    Scanner scan = new Scanner(System.in);
    for (int i = 0; i < 1; i++) {
        MyArray[length] = MyArray[MyArray.length+1];
        System.out.println("Enter Proudact Name : ");
        String Name = scan.next();
        System.out.println("Enter Proudact Price : ");
        int price = scan.nextInt();
        System.out.println("Enter Proudact Stuck : ");
        int Stuck = scan.nextInt();
        System.out.println("Enter Proudact MinStuck");
        int minStuck = scan.nextInt();
        System.out.println("Enter Proudact Id :");
        int id = scan.nextInt();
        Proudact NewProudact = new Proudact(Name,price,Stuck,minStuck, id);
        MyArray[MyArray.length]=NewProudact;
    }
}
fabian
  • 80,457
  • 12
  • 86
  • 114
omer malka
  • 144
  • 1
  • 14

4 Answers4

3

in Java, arrays can not be dynamically enlarged. I suggest you'll use ArrayList instead.

Nir Levy
  • 12,750
  • 3
  • 21
  • 38
3

Why don't you just use a list instead of array because Arrays have static lengths but lists have dynamic.

So instead of:

Proudact[] MyArray = ...

Create new list of type Proudact like this:

List<Proudact> myList = new ArrayList<Proudact>();

And then you can simply add items to list like this:

myList.add(new Proudact(Name,price,Stuck,minStuck, id));

So the full example for your case should look something like this:

List<Proudact> myList = new ArrayList<Proudact>();

myList.add(new Proudact("Milk", 5.55, 50, 10, 0));
myList.add(new Proudact("Chess", 4.35, 50, 10, 1));
myList.add(new Proudact("Apple", 1.25, 50, 10, 2));
myList.add(new Proudact("Wotermelon", 6.21, 50, 10, 3));

public static void NewProd(Proudact[] MyArray){
    Scanner scan = new Scanner(System.in);
    for (int i = 0; i < 1; i++) {
        System.out.println("Enter Proudact Name : ");
        String Name = scan.next();
        System.out.println("Enter Proudact Price : ");
        int price = scan.nextInt();
        System.out.println("Enter Proudact Stuck : ");
        int Stuck = scan.nextInt();
        System.out.println("Enter Proudact MinStuck");
        int minStuck = scan.nextInt();
        System.out.println("Enter Proudact Id :");
        int id = scan.nextInt();
        myList.add(new Proudact(Name,price,Stuck,minStuck, id));
    }
}

Check java collections tutorial here for more details.

Kiki
  • 2,243
  • 5
  • 30
  • 43
0

You are going to have to create a new array of larger size and copy all data from the old array over there. But must you use an array? A Set would be much easier to use

0

Use an ArrayList and then convert it to array if that is what you want in the end.

List<Proudact> proudactList = new ArrayList<>();

populate it with objects of Proudact class & then convert it to array

Proudact[] array = proudactList.toArray(new Proudact[proudactList.size()]);
Abhash Upadhyaya
  • 717
  • 14
  • 34