A friend of mine and I are taking an external computer science test. While studying the review it has become apparent that some of the syntax presented is quite unfamiliar.
Here are a few examples: Stack<Integer> s = new Stack<Integer>(); For this line of code what exactly is the purpose of "<Integer>"?
Does this make it so Stack only returns objects of type Integer?

A question on the review:
"Which of these regular expressions matches both "aggie" and "longhorn". but not "sooner"?
a. [als].*[enr]
b. .*g.*
c. .....(orn)+
d. [^sooner]

The answer sheet says the answer is b. However, I can only make assumptions as to why, and I would like to know if the other answers are valid in syntax and what exactly the symbols mean. What would be helpful are keywords that I could use to perform a search that returns valid results.

Finally, how is a colon to be used with a for loop.
For example:

for(int a : array)
{
       if(a>max) max = a;
}

It appears as if a will take on various values from array. But, I believe I have seen the colon used in a for loop where the realtionship didn't appear as obvious.
What possibilities are there for using a colon in a loop? Is there a name for this?

That seems to sum my questions up. I don't expect explanations of these things (although that would be nice). But I would particularly appreciate it, if anyone was able to give names to these items so I could find more information with that.

Recommended Answers

All 4 Replies

Hmmm. I can't be super helpful before I fall asleep tonight, but..

First thing is working with Generics

Regular expressions? I'm not very good with those.. But you can find tutorials about how to pick them apart / assemble them.

Third thing, the for loop, is called a for-each loop, I think. Basically, yes, the loop moves through each value in the array like with an iterator. for(int a : array) { ... } could be re-written: for (int i = 0; i < array.length; i++) { int a = array[i]; ... } Or, it could be written even more simply.. But I won't go there tonight.

You really need to read a modern beginner's tutorial on Java and an introductory text on regular expressions if you can't answer those questions.
They're so basic any ultimate beginner's text should explain them.

By indicating you don't know what it is, you indicate you lack even basic knowledge, which just a few quick answers here won't provide (you'll just get stuck on the next question, and have to ask again, while doing some study of your own will give you the entire picture).

Thanks for the reply cudmore, I should be able to find the information I need with the links you provided.

Jwenting, after searching through the index of the current book I'm using: "Fundamentals of Java" by Lambert and Osborne. It appears as if Generics, regular expressions, and for-each loops have no mention. So I assume my book is the basics of begginers information on Java. But thanks for your reply, I will look into more recently published books.

Your book is outdated, period.
Those features were introduced into the language several years ago. If you don't use current reference literature, expect to get out of date information.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.