Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

list program error

I am trying below program

public class List<T>{
private T[] datastore;
private int size;
private int pos;
public List(int numElements){

size=numElements;
pos=0;
datastore=(T[])new Object[size];
}

I got below error
List.java:10: error: reached end of file while parsing
}
 ^
1 error


please advise how to fix it. I also did not understand how class name is List and below line

datastore=(T[])new Object[size];

please advise
SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

public class List<T>{
private T[] datastore;
private int size;
private int pos;
public List(int numElements){

size=numElements;
pos=0;
datastore=(T[])new Object[size];
}
}

Open in new window


with two }} also i got comilation error. Is List is not reserved key word. Can we use List as class name?

what is this line meaning
datastore=(T[])new Object[size];
please advise
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Just an advise:
To avoid any naming confusion, - although nothing prevents me from doing it - I would never give my custom classes the name of existing java classes.
So don't call it List, but e.g. MyList or ListOfMine or ListOfObjects or ...
Good, informative class and variable names are key to maintainable code.
Avatar of gudii9

ASKER

That code compiles ok.
yes. it is compiling now
Avatar of gudii9

ASKER

i nver saw class name like below with generic notation.


public class List<T>

i wonder practically why people do not define like above?
Avatar of gudii9

ASKER

and then it is casting it to the type T.  T is a placeholder for whatever type you specify when you create you List object, ie. if you do....   new List<String>();     then T will be String and so your array will get casted to a String[]

i thought when we use generic we do not need to do casting stuff. please advise
i thought when we use generic we do not need to do casting stuff
Generally, that may be the case when you are using a generic class (it's still not a hard rule, but it may be the usual case). However, when you are actually implementing a generic class, it is probably still more likely that you may need to use casting.
>> i thought when we use generic we do not need to do casting stuff.
If T stands for a class Person and you also have the extending classes Man and Woman, you might need to cast if you want to call methods defined on Man or Woman (that are not defined on Person)
E.g. You know you're dealing with a Person (because it's an element out of a List<Person>, but that Person class e.g. has a method isMan(), then you could do:

if (person.isMan()) {
   ((Man)person).typicalManMethod(...);
} else {
  ((Woman)person).typicalWomanMethod(...);
}

Open in new window

Avatar of gudii9

ASKER

public class ListEx<T>{
private T[] datastore;
private int size;
private int pos;
public List(int numElements){

size=numElements;
pos=0;
datastore=(T[])new Object[size];
}
}

Open in new window


i still did not get complete understanding of this class.

Let me say what i understood and you can correct me wherever i am wrong

1. it is a class whose name is ListEx (not sure yet what is use of <T> after class name??)
2. It has 3 class level non static variables as below
private T[] datastore;(not sure what it means by T[], does it mean there is datastore of array object type as class non static variable??)
private int size;
private int pos;

3. It has below cosntructor which takes numElements as arguments

public List(int numElements){

size=numElements;
pos=0;
datastore=(T[])new Object[size];
}


not sure why there are assigning as below??
size=numElements;

not sure what is purpose of below line
pos=0;

not sure yet on below line
datastore=(T[])new Object[size];

please advise
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanx 4 axxepting