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
LVL 7
gudii9Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
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

has two { and only one }
one of the { is unmatched
gudii9Author Commented:
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
mccarlIT Business Systems Analyst / Software DeveloperCommented:
also i got comilation error
That code compiles ok. I'm guessing your compilation error might be somewhere else. What is the error that you are getting?

Is List is not reserved key word. Can we use List as class name?
No, List is not a reserved word. List is a class that is defined inside the "java.util" package, but that doesn't stop you from writing the above which defines the List class to be within the default package, ie. the package with no name. The package is basically a namespace and by having different packages it allows multiple classes to be defined with the same name.

what is this line meaning
datastore=(T[])new Object[size];

If is defining an array that can hold "size" number of Objects (which is ok because everything in Java inherets from Object so anything can be stored in that array) 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[]
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

zzynxSr. Software engineerCommented:
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.
gudii9Author Commented:
That code compiles ok.
yes. it is compiling now
gudii9Author Commented:
i nver saw class name like below with generic notation.


public class List<T>

i wonder practically why people do not define like above?
gudii9Author Commented:
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
mccarlIT Business Systems Analyst / Software DeveloperCommented:
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.
zzynxSr. Software engineerCommented:
>> 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

gudii9Author Commented:
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
zzynxSr. Software engineerCommented:
>> 1. it is a class whose name is ListEx
Correct.
>> (not sure yet what is use of <T> after class name??)
The class can contain instances of whatever class T (T stands for Type)  [cf. https://docs.oracle.com/javase/tutorial/java/generics/types.html]

So you could write this
ListEx<String> myList = new ListEx<String>(15);

Open in new window

or this
ListEx<MyObject> myList2 = new ListEx<MyObject>(10);

Open in new window


>> It has 3 class level non static variables as below
Correct. The variable 'datastore' is an array of elements of type T. It's the "heart" of your ListEx class.
The variable 'size' contains the size of the array.
The variable 'pos' contains the iteration position inside the array.

The constructor takes a 'numElements' parameter, which tells how many items the instance of ListEx will contain.
So, that's why size=numElements;
That is needed to be able to create an array of that length, which is done here:
datastore=(T[])new Object[size];
We're initializing an array of length 'size' (not yet containing any elements). In other words we ask java to reserve memory space to store size elements of type T.

pos = 0;
is just initializing the iteration position at the 'head' of the array.

So, when having
ListEx<String> myList = new ListEx<String>(15);

Open in new window

you create a variable 'myList' of type ListEx which can contain 15 items of type String.

When having
ListEx<MyObject> myList2 = new ListEx<MyObject>(10);

Open in new window

you create a variable 'myList2' of type ListEx which can contain 10 items of type MyObject.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zzynxSr. Software engineerCommented:
Thanx 4 axxepting
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.