Reputation: 154
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;
}
}
Upvotes: 1
Views: 1689
Reputation: 2253
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.
Upvotes: 3
Reputation: 727
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()]);
Upvotes: 0
Reputation: 61
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
Upvotes: 0