Question on import java.util.ArrayList, etc.
Hi,
I was wondering what the following meant and what the differences were. When would I have to use these:
import java.util.ArrayList;
import java.util.Collections; <--I especially don't understand what this means
import java.util.Comparator; <---same for this (can I consolidate these into the bottom two?)
import java.io.*;
import java.util.*;
MAresJonson wrote:
Also, what does this mean:
return foo == f.getFoo() ? true : false;
(more specifically...what does the "? true : false" mean and is there another way to code that?)It's called the ternary operator. For your specific example, you could just do:
return foo == f.getFoo();But, more generally,
return foo == f.getFoo() ? "equal" : "Not equal";means:
if (foo == f.getFoo()) {
return "equal";
else {
return "Not equal";
}As everyone else said at the same time...
Similar Messages
-
How come program wont accept import java.util ?
I get cannot resole symbol ?? i looked in up in my java book and its spelled right . what gives ?there is no class named java.util .
import java.util.ArrayList;
import java.util.EventObject;
or
import java.util.*; -
How to pass attribute of type java.util.ArrayList Property to a Tag
How do I pass an attribute of type, java.util.ArrayList<my.entity.Property> to a Tag implementation class?
Please advise!
Thanks,
Joe
package my.tags;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import javax.servlet.jsp.JspException;
import my.entity.Property;
public class PropertiesTag extends SimpleTagSupport {
private ArrayList<Property> properties;
public void setProperties(ArrayList<Property> properties) {
this.properties = properties;
public void doTag() throws JspException, IOException {
<?xml version="1.0" encoding="utf-8" ?>
<taglib ...>
<tag>
<name>propertiesTag</name>
<tag-class>my.tags.PropertiesTag</tag-class>
<body-content>empty</body-content>
<description>Displays the product selection left menu</description>
<attribute>
<name>properties</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<type>java.util.ArrayList<my.entity.Property></type>
</attribute>
</tag>
</taglib>Here's the error message:
org.xml.sax.SAXParseException: The element type "my.entity.Property" must be terminated by the matching end-tag "</my.entity.Property>".802826 wrote:
How do I pass an attribute of type, java.util.ArrayList<my.entity.Property> to a Tag implementation class?
Please advise!
As already pointed out, there is no way to specify a generic type in a tag library descriptor. You may however specify an Object type in your tld and still have the variable in your tag as a parameterized generic type.
In your tld change the type to Object.
<type>java.lang.Object</type>.
The properties tag itself needs no change and can continue to use parameterized types.
cheers,
ram. -
A problem with importing java.util.concurrent
I am rather new to Java with not that much experience. I apologize ahead though if this is the inappropriate forum to post this problem and/or this question has been answered somewhere else (I couldn't find a post on this subject).
Some of my research has lead to using a semaphore if you wish to use the synchronized() method. I looked up that the import for that is under java.util.concurrent.Semaphore; however, thanks to netbeans for pointing this out before I compiled it, this doesn't seem to exist.
I'm using Java version 1.6.0_03 which does seem to be the latest version. Was it simply removed or am I seriously missing something big here? Anyways, I'm at a total loss and hopefully someone can point me in the right direction.
Thank you in advance for your help.jiju wrote:
check whether import java.util.concurrent.*; is workingAwesome.
As for netbeans, I went and double checked to see if it is reading from the most updated folder of Java and it is.
So as I said, I am totally lost as to why it's not working. Should I just downgrade to a lower version of Java? Although it would seem kinda weird to do something like this. -
Hi,
In my program I hava inported the java package like
import java.util.*;
import java.sql.*;
import java.text.*;
then for using some classes like Gregorial calender,Date etc,I have to again import
import java.util.Date;
import java.sql.PreparedStatement;
import java.util.GregorianCalendar;
As far as I know if I have import the above like
import java.util.*; then it means I can use any classes in java.util ,but if I am not importing
import java.util.GregorianCalendar;
import java.util.Date;
my program is giving error.Can somebody tell me,where my concept is wrong.
Thanksimport java.sql.*;
import java.util.*;
//import java.util.Date; // <-- needed to avoid "the type Date is ambiguous"
public class PackageEg {
public static void main(String[] args) {
Date test = new Date();
}This code gives an error when I compile it, but if I include the
java.util.Date import it is good. The reason is that there are two
possible Date classes - one in java.util and one in java.sql. The
compiler cannot figure out which one I mean.
If I include a specific class like java.util.Date, then this one will be used
in preference to any .* imports. (These are called "imports on demand")
This explains why you need java.util.Date, but the other two are a
mystery. As far as I know PreparedStatement and GregorianCalander
are unique names within the packages you mention. Could you post
some code that will not compile unless the specific import statements
are there? -
By benefit I mean does it reduce compile time or have some other beneficial side effect to import only those classes you're using in a class rather than importing the package they reside in.
For example:
// this?
import java.util.Vector;
// or this..
import java.util.*;
class Test
Vector v;
}Question is strictly academic...I'm just curious.I like Dr. clap's answer. Also, though you can
consider code readability. Obviously importing a
whole package doesn't give you an idea of what classes
are being used. "I", personally, have only seen exact
class imports done when learning something new. This
is basically just for clarity, and not speed as Dr.
Clap pointed out.That's probably a good thing to do for examples, but I can't see why you would want to know what individual classes are being used otherwise.
From a programming standpoint if you're going to build a complex dialog you don't want 500 import statements. If you know you're going to be using lots of classes from an individual package might as well import the whole thing. -
Cannot import java.util.concurrent.locks ... WHY?
Why is Xcode unable to find the java.util.concurrent.locks package. The class browser knows it exists. For example the entry for the ReentrantLock class looks like this in the browser class window:
ReentrantLock (java.util.concurrent.locks)
Xcode knows about other java.util packages such as java.util.ResourceBundle which I have been accessed successfully in other parts of my projecgt.
Here is a source file and the resulting compiler error:
The source file:
// Foo.java
import java.util.ResourceBundle;
import java.util.concurrent.locks;
public class Foo { }
The compiler error:
compile:
Compiling 2 source files to /Users/Terry/Desktop/JAVA/PROJECTS/Logic/bin
/Users/Terry/Desktop/JAVA/PROJECTS/Logic/src/Foo.java:3: cannot find symbol
symbol : class locks
location: package java.util.concurrent
import java.util.concurrent.locks;
^
1 error
BUILD FAILED
Help or hints would be greatly appreciated!Well the reason to your problem is very simple... java.util.concurrent.locks is a package... Not a class.
if you want to import a specific class, the class should be written at the end like you did for import java.util.ResourceBundle; but if you want to import a whole package you need to add the little star at the end :
import java.util.concurrent.locks.*;
Or else, you only import the class you need :
import java.util.concurrent.locks.ReentrantLock; -
USage of import java.util.* & using another package
When we use import java.util.*; from where does JAVA take the class that are related to util, when I did search for "util" in my machine, I got about 50 results for folders named util and also some where in a folder called "java" when I opened it it did not have the classes, that I was expecting. So I am not clear where does it get the class files from. This is not only for this but for any import commands like import java.io.*;
When we get another package or source code which was written in java and want to use it in our code where do we have keep the files ? eg. i have the java code that i m writing in c:\java then do I put all the downloaded related files in that folder and then run the code ? or i keep a folder inside java folder and then import them into my code ?
please do advice.
Thanks in advance.When we use import java.util.*; from where does JAVA
take the class that are related to util,It means when you use a classname in your code, and that classname is neither in the same package as the class you're writing, nor imported specifically by classname, that it will search for it.
Where it searches is every folder called util whose parent folder is called java which in turn is at the root level of one of the classpath elements. That is, if classpath contains "A" and "B", and you refer to a class called "Foo", then it will look for "A/java/util/Foo.class" and "B/java/util/Foo.class".
search for "util" in my machine, I got about 50
results for folders named util and also some where in
a folder called "java" when I opened it it did not
have the classes, that I was expecting. So I am not
clear where does it get the class files from. This is
not only for this but for any import commands like
import java.io.*; All the java.* and javax.* and whatever else it part of the core API is in rt.jar, which lives inside the Java distribution. Classpath elements can be directories or jar files or zip files. -
The import "java.util.scanner" is not vaild
I am experiment with the java.util.scanner API after compiling the code below I received the following error:
The import "java.util.Scanner" is not valid, since it does not name a type in a package.
I declared a string variable should that suffice?
import java.util.Scanner;
public class Echo {
public static void main (String[] args)
String message;
Scanner scan = new Scanner (System.in);
System.out.println ("enter something:");
message = scan.nextLine();
System.out.println ("hey you entered: \"" + message + "\"");
}Most IDEs let you select and installed JDK for them to use. Check out that IDE's preferences.
Otherwise, start here and learn to do it with a simple text editor or programmer's editor, and the command line.
http://java.sun.com/docs/books/tutorial/getStarted/cupojava/ -
Compile error import java.util.map$entry
Hi,
I am trying to compile code which imports the following package
import java.util.Map$Entry.
The error thrown up is : cant resolve symbol Map$Entry. Why is there a $ in the package import path and is there some configuration required to compile the file.
I am not allowed to change the code. Does anyone have an idea on how this problem can be solved.
Thanks and regards
Kumar VellalBtw java docs for the interface are available at:
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Map.Entry.html
Changing the import should probably help compile the code, but without changing the import .. need to check. This is just from what i remember now. Lets see if anyone else comments on this. -
Error when importing java.util.Scanner
Agh! I'm in an introduction to computer science course, and I am writing a program as an assignment that's due tomorrow.
I have imported java.util.Scanner before, but I've only run it on the Windows PCs in the lab, whereas I am currently on a Mac. I've updated to the latest version of Java (through the software update), and I'm running Tiger (also fully updated).
I'm using Dr. Java to write the programs, but I tried using the very same thing in Eclipse and it didn't work. Here's my program:
import java.util.Random;
import java.util.Scanner;
public class Password {
public static void main(String[] args) {
//create a Scanner object to read from the keyboard:
String password;
Scanner scanner = new Scanner(System.in);
Random randomizer = new Random();
System.out.println("Please enter a string containing candidate characters.");
String input = scanner.next();
int charLength = length(input);
System.out.print("Random password: ");
System.out.print(input.substring(randomizer.nextInt(charLength)));
System.out.print(input.substring(randomizer.nextInt(charLength)));
System.out.print(input.substring(randomizer.nextInt(charLength)));
System.out.println(input.substring(randomizer.nextInt(charLength)));
And, here's my errors:
4 errors found:
File: /Users/brianmoore/Desktop/Password.java [line: 11]
Error: cannot resolve symbol
symbol : class Scanner
location: package util
File: /Users/brianmoore/Desktop/Password.java [line: 18]
Error: cannot resolve symbol
symbol : class Scanner
location: class Password
File: /Users/brianmoore/Desktop/Password.java [line: 18]
Error: cannot resolve symbol
symbol : class Scanner
location: class Password
File: /Users/brianmoore/Desktop/Password.java [line: 22]
Error: cannot resolve symbol
symbol : method length (java.lang.String)
location: class Password
Any ideas?Ok. I figured it out. On the Dr. Java page, this is what helped me:
Please verify the following:
- Open "/Applications/Utilities/Java/J2SE 5.0/Java
Preferences";
make sure "J2SE 5.0" is at the top of the list under
"Java
Application Runtime"
- Open DrJava; go to Edit->Preferences; make sure
"Tools.jar
Location", "JSR-14 Location", and "JSR-14
Collections Path"
are all blank
- Open the Help->About dialog box; make sure the "DrJava
Version" listed is 20050601-0007 or later (that is, later
than
June 1, 2005)
- Go to the "System Properties" tab in the
"About" box; make
sure "java.version" is 1.4.2.
Let us know what you find out if you're still having
trouble.
Thanks for your help! -
Import java.util.Enumeration;
Hi, I test a programmer under eclipe
I can import java.util.Enumeration;(error on Enumeration)
and Vector connections = null;
Enumeration enum = connections.elements();
while (enum.hasMoreElements())
do something
I got error Enumeration can't not be resolve/
why?
and how to modify to change it?Hi, I test a programmer under eclipe Maybe should test the program, not the programmer ;-)
-
Newbie help with collection java.util.ArrayList
Hi,
the documentation for the class ArrayList is given as
public class ArrayList<E>
extends AbstractList<E>
what is that <E> over there ? Similarly, what is the <T> in an iterator ? Exact problem along with code is at the end of the post.
I am a C++ programmer and am very comfortable with the STL, but that approach i.e. ArrayList<Job> does not work over here :(
PLease help.
TIA,
Madhu.
Code Details:
I have a class (Job) which I wish to add to the list using the add method. Obviously, it gives an error.
Code goes as
class MyQueue extends java.lang.Object
private java.util.ArrayList jobsList=new java.util.ArrayList(100);
public void addtoMyQueueatIndex(Job newJob,int Index)
jobsList.add((java.lang.Object)newJob,Index);
Error is :
MyQueue.java:96: cannot find symbol
symbol : method addtoMyQueueatIndex((java.lang.Object,int)
location: class java.util.ArrayList
jobsList.add((java.lang.Object)newJob,Index);Are you using J2SDK 1.5.0 beta 1 or J2SDK 1.4.2? Are you using the docs from 1.5 and the compiler from 1.4.2?
private java.util.ArrayList jobsList=new java.util.ArrayList(100); //-- 1.4.2 code
private java.util.List<Job> jobsList = new java.util.ArrayList<Job> (100); //-- 1.5.0 code (preferred style)
private java.util.ArrayList<Job> jobsList = new java.util.ArrayList<Job> (100); //-- 1.5.0 codeAs a matter of style, use the interface and not the real class when declaring a variable like the jobsList above whenever possible or applicable.
You do not need to cast the object newJob to Object. And you have inverted the positions of the parameters.
void add(int index, E element)
Inserts the specified element at the specified position in this list (optional operation).
jobsList.add(Index, newJob); -
I am trying to retrieve a list from coherence cache and when I try to test my cache I get the following exception
Exception in thread "main" (Wrapped) java.io.IOException: Deserialization failed, format CollectionFormat(public java.util.ArrayList())
at com.tangosol.util.ExternalizableHelper.fromBinary(ExternalizableHelper.java:266)
at com.tangosol.coherence.component.util.daemon.queueProcessor.service.grid.partitionedService.PartitionedCache$ConverterFromBinary.convert(PartitionedCache.CDB:4)
at com.tangosol.util.ConverterCollections$ConverterMap.get(ConverterCollections.java:1655)
at com.tangosol.coherence.component.util.daemon.queueProcessor.service.grid.partitionedService.PartitionedCache$ViewMap.get(PartitionedCache.CDB:1)
at com.tangosol.coherence.component.util.SafeNamedCache.get(SafeNamedCache.CDB:1)
at com.sesco.cohc.cohc_RegionalForecastedGen.getLatestDataForDataSource(cohc_RegionalForecastedGen.java:32)
at com.sesco.cohc.cohc_RegionalForecastedGen.main(cohc_RegionalForecastedGen.java:172)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.io.IOException: Deserialization failed, format CollectionFormat(public java.util.ArrayList())
at org.gridkit.coherence.utils.pof.ReflectionPofSerializer.internalDeserialize(ReflectionPofSerializer.java:105)
at org.gridkit.coherence.utils.pof.ReflectionPofSerializer.deserialize(ReflectionPofSerializer.java:92)
at com.tangosol.io.pof.PofBufferReader.readAsObject(PofBufferReader.java:3316)
at com.tangosol.io.pof.PofBufferReader.readObject(PofBufferReader.java:2604)
at com.tangosol.io.pof.ConfigurablePofContext.deserialize(ConfigurablePofContext.java:368)
at com.tangosol.util.ExternalizableHelper.deserializeInternal(ExternalizableHelper.java:2746)
at com.tangosol.util.ExternalizableHelper.fromBinary(ExternalizableHelper.java:262)
... 11 more
Caused by: java.io.IOException: Failed to deserialize java.util.ArrayList instance
at org.gridkit.coherence.utils.pof.ReflectionPofSerializer$CollectionSerializer.deserialize(ReflectionPofSerializer.java:317)
at org.gridkit.coherence.utils.pof.ReflectionPofSerializer.internalDeserialize(ReflectionPofSerializer.java:101)
... 17 more
Caused by: java.io.EOFException
at com.tangosol.io.AbstractByteArrayReadBuffer$ByteArrayBufferInput.readPackedInt(AbstractByteArrayReadBuffer.java:430)
at com.tangosol.io.pof.PofBufferReader$UserTypeReader.complete(PofBufferReader.java:3819)
at com.tangosol.io.pof.PofBufferReader.readCollection(PofBufferReader.java:2452)
at org.gridkit.coherence.utils.pof.ReflectionPofSerializer$CollectionSerializer.deserialize(ReflectionPofSerializer.java:306)
... 18 more
I am not sure why I am getting the error. Any help would be appreciatedYou were using the ReflectionPofSerializer coded by Alexey. If you are pretty sure that the object you retrieved was stored using the same serializer, might want to contact Alexey to see if he can help you out.
ReflectionPofSerializer - gridkit - ReflectionPofSerializer project page - A home for code misc usefull code related … -
Hi,
I'm getting following error :
Error(86,88): incompatible types; found: java.util.ArrayList, required: com.sun.java.util.collections.ArrayList
The line JDev is complaining about contains the following code :
com.sun.java.util.collections.ArrayList runtimeErrors = container.getExceptionsList();
I really don't have a clue where this error comes from. If I right-click on ArrayList it brings me to the correct declaration.
Does somebody know what I'm doing wrong?
Thanks in advance for you help!
KrisKris,
try changing the code to :
java.util.ArrayList runtimeErrors = container.getExceptionsList();apparently container.getExceptionsList() returns a java.util.Arraylist not a com.sun.java.util.collections.ArrayList
HTH,
John
Maybe you are looking for
-
Deleted my firefox sync account and now can't get back in.
I recently upgraded to firefox 12 which appeared to break my firefox sync and would not allow me to re-pair my PC or any other devices to my account. In order to try fix the problem, I decided to delete my account. Having done this, I created a new s
-
Why doesn't the App Store prompt for older versions on first download?
I thought Apple user interfaces were supposed to be slick. Here's what I have to do to install an app. I've inherited a first generation iPad from a friend. The iPad was fully erased and set back to factory defaults before I got it. I have create
-
How to read the contents of images folder using AS3
Hi, I'd like to load the images in the folder dynamically without using XML. Is there a mechanism in AS3 that enables me to read directly the content of images folder directly without using XML or should I use php to do that? Thanks
-
++Urgent++IMP++ErrorError during rendering of page item
Error Error during rendering of page item P2_SERVICE_DESCRIPTION. ORA-06502: PL/SQL: numeric or value error: character string buffer too small Technical Info (only visible for developers) is_internal_error: true apex_error_code: WWV_FLO
-
Photoshop cs6 install administrator error - Windows 7 64-bit
I have attempted installation of Photoshop CS6 repeatedly following a fresh installation of Windows 7 64-bit. Initially running into the error stating Administrator Rights is required to run this upgrade image, I forced it to Run as Administrator. I