Suppose that you have an ArrayList and that it contains String objects. Which declaration of the ArrayList requires that objects retrieved using the get method be cast to Strings before calling a String method?
I. ArrayList a = new ArrayList();
II. ArrayList<Object> a = new ArrayList<Object>;
III. ArrayList<String> a = new ArrayList<String>;
A. I only
B. II only
C. III only
D. I and II only
E. I, II, and III
I know that all of these are ways to declare an Array List, but I am unfamiliar with the last two since I usually just declare my Array Lists with the first option.
Im making a simple code to add an array to a List (the code im referring to is <String> )
import java.util.*; public class L5_ArrayListProgram { public static void main(String[] args){ String[] things = {"lasers","ghouls", "food", "dark"}; List<String>list1 = new ArrayList<String>(); for(String x: things) list1.add(x);
My simple question is - what are the <String> ...<String> for? I understand it makes the list1 variable a string, but why is it made like this? do we usualy use <String> when we need to make a variable a String?
this is part of a larger project but i figure if i can figure out this first step i can work the rest. I need to get a title from the user add it to a object array (i think thats what it is) and then when i call listAllItems(), items[] should be copied into a string array called listAllItems, and then printed, but currently im jusst having trouble i think making it an object and then make it an item in the items[], if i run case 3 the list it says null...
public class MediaItem { String title; MediaItem(){ } MediaItem(String title){
I am trying to count the number of occurrences of a string in an array list. I used the following code:
int count = Collections.frequency(strings, search);
strings is the name of the array list and search is the string that I am trying to count the number of occurrences of. My program compiles correctly, but when I enter the string to search for, I get the following error: "Exception in thread "main" java.lang.NullPointerException at java.util.Collections.frquency(Collections.java:37 18)
Why am I getting this error message and how do I fix it?
I have some class called sorted to sort the linked list through the nodes of the list. and other class to test this ability, i made object of the sort class called "list1" and insert the values to the linked list.
If i make other object called "list2" and want to merge those two lists by using method merge in sort class. And wrote code of
list1.merge(list2);
How can the merge method in sort class know the values of list1 that called it as this object is created in other class.
I am new to Java and have read books, the Java docs, and searched the Internet for my problem to no avail. I have an Array of objects that contains strings. How can I get the object's strings to print in a list so that the user can select that object to manipulate its attributes? For example, the user can select "Guitar 1" from a list and manipulate its attributes like tuning it, playing it, etc. I have a class called Instruments and created 10 guitar objects.Here is the code:
Instrument [] guitar = new Instrument[10]; for (int i = 0; i < 10; i++) { guitar[0] = new Instrument("Guitar 1"); guitar[1] = new Instrument("Guitar 2"); guitar[2] = new Instrument("Guitar 3"); guitar[3] = new Instrument("Guitar 4"); guitar[4] = new Instrument("Guitar 5"); guitar[5] = new Instrument("Guitar 6");
I have just started working with linked lists. I have a linked list of Objects and I want to be able to search for a specific object. But currently my code continues to return false. Also how would I go about removing the first index of the linked list.
public static void main(String[] args) { LinkedList<Cookies> ml = new LinkedList<>(); int choice = 0; while (choice >= 0) { choice = menu();
lst contains list of objects and one of the objects contains the property bDate(Timestamp) which has the value 28-2-1989 00:00:00.0, now I just wants to change the value into 28-2-1989 and store it back into the List as a Timestamp. how can I do that.
I am following those three tutorials and I have completed it with success.
( [URL] .... ) ( [URL] .... ) ( [URL] .... )
But then, as author haven't implemented removeCountries method I tried to create it. What I did initially was to just add to class Countries this method:
public boolean removeCountry(Country country) { return countries.remove(country); }
But although compiler wasn't complaining it didn't work. Actually it worked last night (before reboot) but not today. Must be some SOAP iterator/binding thing or whatever. Or I thought that it worked but in fact it didn't.
Here are original classes:
//------------------------------- public class Country { String CountryId; String CountryName; public Country() { super();
[Code] ....
I would like to avoid my own iterator as JDeveloper can generate automatically iterators for webservices, but if I can't get it that way, what would be better way to write above mentioned iterator in removeCountry method?
Is there any way to remove object directly with something like this:
co.countries.remove(o); co.removeCountry(country)
using method
// This left unused public boolean removeCountry(Country country) { return countries.remove(country); }
I want to create a program where I need to create an object of list type such as text file will contain nos like 1,2,3,4,5 and write into text file and delete the in FIFO order i.e 1,2,3,4,5...how i can achieve to write a program? I tried bt everytime got concurrent modification exception or Array out of bound exception.
I am trying to put a reference to a given subclass object into a linked list, and then come back later, and invoke a method of the subclass object that is in a given spot in the linked list. This produces an error because Object does not have that method. Is it necessary to cast the object to the correct subclass every time I want to use one of its methods, or is there a way to convince the JVM to treat it as always of type MySubclass?
How to use the id parameter in my documents entity to download documents from a list of documents. Normally I use ListDataModel and the getRowData method. I would like to know how to achieve the same thing using an ordinary List object.
My list of documents is called List<CountryDocs> selectedDocs;
Clicking on the download link calls the following method in my managed bean:
@ManagedBean(name = "countryDocBean") @SessionScoped public class CountryDocBean { private List<CountryDocs> selectedDocs; public StreamedContent getDownloadedFile() {
[Code] ....
Debugging shows the value for the id is 0 and this results in a NullPointerException. I've tried several methods for grabbing the document id in my backing bean, but no luck yet. I also read about the the ViewParams and ViewAction method but they caused validation errors to do with the <f:metadata> tags. I don't know how to obtain this value using a normal List object.
I have an Array of objects that contains strings. I am new to Java and have read books, the Java docs, and searched the Internet for my problem to no avail. How can I get my strings to print in a list so that the user can select an object to manipulate its attributes? I have a class called Instruments and created 10 guitar objects. Here is the code:
Instrument [] guitar = new Instrument[10]; for (int i = 0; i < 10; i++) { guitar[0] = new Instrument("Guitar 1"); guitar[1] = new Instrument("Guitar 2"); guitar[2] = new Instrument("Guitar 3"); guitar[3] = new Instrument("Guitar 4");
We were suppose to make a program for an assignment and the prof provided some codes to start of. What does this basically mean? How do i access the string arrays from consolelist?
class ConsoleInfo { private String conTitle; private double conPrice; private int conQty; private String conPic; private static String empPassword; ConsoleInfo(String title, double price, int qty,String pic)
I am looking for a good and reliable library to read a string to construct a list of Integers, Doubles, Booleans, etc. This library should be robust enough to handle faulty input from an inexperienced user.
I am trying to get a price of a certain show in a theatre but the problem is that i don't know how to use the method's String parameter:
/** * Gives the price of the show with a given place in the room * * @param place the place in the room (VIP, average seating, in the back) * @return price */ public double getPlaceprice(String place) { if (place !=null) { double p = place.getPrice(); // price= Double.parseDouble("place.getPrice()"); return p;
} return 0; }
Now I have a class called Place which has an attribute double price and a getter for price
public double getPrice() { return price;
So I somehow have to alter the String parameter Place to the object(class) Place, on which i can call the method getPrice()
At least, that's what i think i have to do. Where i go wrong?
I have the following method that I need to implement:
{ // YOUR CODE HERE File file = new File(filename); int counter = 0; String tempArtist, tempName; Album tempAlbum; Track tempTrack;
[code]....
Ok so I updated my code from the initial post since I made some progress on my own. I guess now I'm just stuck on how to scan in the file of strings and stock it into the type Track. (I've tried using both the initial linked list for this and a temporary variable with no luck).
Code a Java method that accepts a String array and a String. The method should return true if the string can be found as an element of the array and false otherwise. Test your method by calling it from the main method which supplies its two parameters (no user input required). Use an array initialiser list to initialise the array you pass. Test thoroughly.
public class test { public static void main(String[] args) { Printhelloworld(); String[] verbs = {"go", "do", "some", "homework"}; printArrays(verbs);
How do I compare a String to each element of a string array?
For example:
int headscount = 0; if (coins[i].equals("heads")){ headscount++; System.out.println("b" + headscount); }
This doesn't give me the right value because the IDE says that equals() is an incompatible type. I also tried changing the "heads" to an variable, but the results remains the same.