hello All, I have a class Item to and I want to create array of this. If I do Item items[3]; items[0]= new Item(); ..... items[0].setItemID(1); .... is this not correct... Please help Thanks adithi
Thanks for the quick replies..But if I do like this... Item[] items = new Item[3]; items[0]= new Item("ItemID1","ItemName1"); it gives me compiler error why so? Thanks adithi
It's hard to say why without seeing the code. Maybe, you're not matching the Item constructor's arguments, maybe, Item class is not visible from where you wanna create it, maybe... 1000 and one reasons. Examine the compiler error carefully, it must have a good hint.
Hi I get this message when I compile it.. First.java:13: ']' expected items[0]= new Item("ItemID1","ItemName1"); ^ First.java:13: <identifier> expected items[0]= new Item("ItemID1","ItemName1"); ^ First.java:13: cannot resolve symbol symbol : class emp location: class First items[0]= new Item("ItemID1","ItemName1"); ^ 3 errors I have declared a constructor Item(String ID, String Name) so this should work I assume... Thanks Lakshmi.
If you post your code, we'll see the error clearer. Make sure class emp is accessible from class First, make sure that the array of Items is created inside the scope of the assignment.
Hey, The problem is dat u have just created an array refrence but u have'nt created and array object yet. i mean u have done this: Item items[3]; \\ Array refrence created but wont compile coz u \\shouldn't specify lngth der Item items[] = new Item[3]; \\ Array Object created. Untill u do not create an array object u would not b able to use d array itself. Then u can create ur object like: items[0] = new Item; \\ Your object created. Then u can use it like: items[0].setItemID(1); I hope diz mght help you. Bye. Kind Regards, Anish Doshi.
Hi all, This is my program. I want to create an Array of Item and use the constructor I have ( 2 parameters ).
------------------------ public class Temp { public static void main(String args[]) { Item item1= new Item(1,"Item 1"); System.out.println(" the Item I details are "+ item1.getItemID()+".\n The info is "+item1.info); } }
class Item { private int ItemID; public String info;
public Item(int ID, String info) { ItemID= ID; this.info=info; }
public int getItemID() { return ItemID; } } ---------------- I want to add some thing like Item[] items= new Item[3]; but I want to initialize the elements also to different values. I checked different books but could find no information about this. Thanks much Adithi.
check this ... ---------------------------------- public class TempItem { public static void main(String args[]) { Item arrItem[] = new Item[3]; for(int i=0;i<arrItem.length;i++) { arrItem[i] = new Item(i,"Item "+i); System.out.println(" the Item "+ i +" details are "+ arrItem[i].getItemID()+".\n The info is "+arrItem[i].info); } } }
class Item { private int ItemID; public String info; public Item(int ID, String info) { ItemID= ID; this.info=info; }
public int getItemID() { return ItemID; } } ----------------------------------
Anbudan & Mahalo,<br />Arul<br /> <br />-Not a sun certified Java professional :-)
Thanks Michael, One more questions, If I dont declare the constructor and want to use the default one. then saying Item items = new Item[3]; ... would do all the work right? I dont have to do anything else. Thanks again for you help adithi
It would give a normal human mental abilities to rival mine. To think it is just a tiny ad:
Gift giving made easy with the permaculture playing cards