I have a ArrayList made up of different elements imported from a db, made up of strings, numbers, doubles and ints. Is there a way to use a reflection type technique ... |
I have an array that is initialised like:
Element[] array = {new Element(1),new Element(2),new Element(3)};
I would like to convert this array into an object of the ArrayList class.
ArrayList<Element> arraylist = ???;
I am ... |
How do you go about comparing two generic classes?
class Entry<K,V>
{
protected K key;
protected V value;
public K getKey() { return key; }
public V getValue() { return value; }
public ...
|
We have:
class A{}
class B extends A{}
class C extends B{}
class D extends C{}
we can define Lists like:
List<? super B> lsb1 = new ArrayList<Object>();
//List<? super B> lsb2 = new ArrayList<Integer>();//Integer, we expect this ...
|
I have been using vectors in the past and am very familiar with them. I have heard that ArrayLists are faster and more flexible. I am new to using |
I know why one shouldn't do that. But is there way to explain to a layman why this is not possible. You can explain this to a layman easily : Animal ... |
I am new to Java, so I am not aware of the nitty gritties.
Why can't I create generic array of parametrized ArrayList?
Instead I have to write,
ArrayList<String>[] alist = new ...
|
|
I've read similar questions here but I'm still a little confused.
MyCollection extends ArrayList<MyClass>
MyClass implements Data
yet this gives me the "cannot convert from ArrayList to MyCollection"
MyCollection mycollection = somehandler.getCollection();
where ... |
I'm having a problem that I have encountered before, but I still don't know why it happens.
This is the code:
package Program;
import java.util.ArrayList;
import java.util.Iterator;
/**
* This class will hold the full collection ...
|
I see this syntax a lot and don't understand the reasoning behind it. I thought you generally want to work with classes rather than interfaces to make it easier to carry ... |
It is known that arraylist init. should be like this
ArrayList<A> a = new ArrayList<A>();
ArrayList<Integer> a = new ArrayList<Number>(); // compile-time error
so, why does java allow these ? ... |
I was just playing around and a thought came to my mind and I decided I want to try it:
Make an ArrayList that holds more ArrayLists.
For example, I created an ArrayList ... |
How To Instantiate a java.util.ArrayList with Generic Class Using Reflection? I am writing a method that sets java.util.List on target object. A target object and a generic type of list is ... |
I have a two-dimensional ArrayList to store Block objects to use later. However, it won't let me call Block methods on the objects when I get them by their index in ... |
I am using an Arraylist to hold heterogeneous data...a mix of String, int and class objects (These objects could be instances of any class I've defined.). I've initialised the ArrayList as ... |
I'm creating a Table class that uses an ArrayList of ArrayLists, and when the user instantiates the class, I want them to be able to create a table of Strings or ... |
I have always had this one issue with arrays of ArrayLists. Maybe you can help.
//declare in class
private ArrayList<Integer>[] x;
//in constructor
x=new ArrayList[n];
This generates a warning about unchecked conversion.
But
x=new ArrayList<Integer>[n];
is a compiler ... |
I don't understand why the following does not work:
public void doSomething(int... args){
List<Integer> broken = new ArrayList<Integer>(Arrays.asList(args))
}
Its my understanding that the compiler converts the "int... args" to an array, so ... |
I'm working in the android environment and have tried the following code, but it doesn't seem to be working.
String [] stockArr = (String[]) stock_list.toArray();
If I define as follows:
String [] stockArr = ...
|
Why can't I create an array of List ?
List<String>[] nav = new List<String>[] { new ArrayList<String>() };
Eclipse says "Cannot create a generic array of List"
or
ArrayList<String>[] nav = new ArrayList<String>[] { new ...
|
I'm new in Java, and I have seen a ArrayList example like this.
listing = new ArrayList<Lot>();
I know that if I want to create an empty array list. Then I ... |
I have an array of String which correspond to the names of Classes in one of my packages.
For example, lets say I had an ArrayList that contained three items: "A1", "A2", ... |
I have implemented array list with arrays:
public class ArrayIndexList<E> {
private E[] A;
private int capacity = 5; // Initial Array Size
...
|
I have a HashMap where the values are ArrayLists, and I'm trying to write a function to accept generic instances of these HashMaps
HashMap<String, ArrayList<Integer>> myMap = new HashMap<String, ArrayList<Integer>>();
public static void ...
|
I know what ArrayList<T> is used for, but when should I use ArrayList<?> ? can you explain with example? thanks.
|
I'm attempting to do something that seems rather simple: sum the sizes of a list of sets. Netbeans gives the following warning/error:
actual argument java.util.ArrayList<java.util.TreeSet<java.lang.Integer>> cannot be ...
|
i am returning results from a sql query which contains multiple rows of three columns.
Column1(a rate) must be multiplied by column2(a numerical value). Column 3 consists of five different possible values. ... |
i want to use Hamcrest’s hasItems with an "actual" collection that is an ArrayList<? extends SomeInterface>
on
assertThat(ArrayList<? extends SomeInterface>, hasItems(InstanceOfSomeInterface))
the compiler says:
The method assertThat(T, Matcher<T>) in the type Assert ... |
I have this code, which compiles:
new TypeToken<ArrayList<ServerTask>>() {}.getType()
Then I have tried
ArrayList<ServerTask>.class
which does not compile.
I am new to Java programming (came from C#) and I have thought that T.class is an exact ... |
I am using generics in a certain data structure. I have to store int x, int y, and Value value, where value is the generic type.
I am trying to group all ... |
Is there a way to subclass the ArrayList class to only allow objects of a specific class (or subclass thereof).
Specifically, I have a base class called RecordStatus and I need to ... |
List<EventHandler<E>> listeners = new List<EventHandler<E>>();
Why the line above fails with:
Cannot instantiate the type List<EventHandler<E>>
But this one works:
ArrayList<EventHandler<E>> listeners = new ArrayList<EventHandler<E>>();
|
An assignment has asked us to implement a Bag data type using either ArrayList or LinkedList as the backend. I've picked the ArrayList and created my own implementation of it, ... |
I'm having an issue understanding some compiler-errors, regarding 2D arrays (ArrayList containing an ArrayList) and generics. My understanding of generics isn't the best, so I tried to research the issue beforehand ... |
I have been writing some code just to make sure I understand generics. I am using generics so this code can deal out regular playing cards or UNO playing cards. A small snippet of my code: public T dealCard () { T card; int index; index = rand.nextInt(drawDeck.size()); card = drawDeck.get(index); drawDeck.remove(index); return card; } However when I call this method ... |
After great effort on a long - flat climb, I am suddenly recoding the core of my long awaited recode. Can somebody help me decipher this ? T[] toArray(T[] a) This is from src_jdk1.5.0_12\java\util\ArrayList.java and is intended to fill a recently new'd ArrayList using copy semantics with the code: synchronized void getWords(java.util.ArrayList wordData) Which will be called from another part ... |
|
Hello and thanks in advance for your help... I am running into a problem trying to sort an ArrayList of objects. First the Objects I'm trying to sort look like (paired down to the essentials for this post): public class Node implements Comparable { int value; public Node(int i) { this.value = i; } public int compareTo(Object o) { if (! ... |
Below given code compiles and runs fine but sorting is not done. please help. import java.util.*; import java.util.Scanner; import java.io.*; class DVDInfo2 implements Comparable; { private String m_title; private String m_genre; private String m_leadActor; DVDInfo2(String t,String g,String a) { m_title = t; System.out.println("title"+m_title); m_genre = g; System.out.println("genre"+ m_genre); m_leadActor = a; System.out.println("leadActor"+m_leadActor); } public String getT() { return m_title; } public ... |
|
Hi, is there any simple way to cast an array to a List. I am using a DAO library that returns an Array of objects that I have to loop and recreate into a List everytime I get data Users[] users = dao.findUserName(searchStr); I would like to use something like List users = (List)dao.findUserName(searchStr); I am looking for a simple way ... |
hi i have an arraylist ArrayList data= new ArrayList(); data.add("goodbye"); Iterator it = data.iterator(); while (it.hasNext()) { String s = it.next(); System.out.println(s); } then i iterate it and print the value now if i change my generic to Integer type what will happen initially the code gets compiled but after that i am changing it to ArrayList data= new ArrayList(); i ... |
|
I have to define my own stack and store the elements of the stack into an ArrayList. My stack should have at least the methods push() that adds an element at the next available position in the ArrayList and and pop() that will get the last item of the ArrayList until the first one. I am not asking anyone to do ... |
I have been working with Java for about a week. I first created a class and a few objects. I then created a class which provided an ArrayList of these objects with a few methods of my own. All of this code compiled without errors or warnings. I then realized that I had an ever increasing set of methods which just ... |
Hello, I precise I'm THE newbee So I would like to know how I can have the type of an ArrayList. I have: - an Object Machine in which I have some ArrayList (ArrayList, ArrayList, ArrayList.... - an Object Parc in which I have an ArrayList So I would use a generic method to have all the tree of my parc. ... |
sorry for late reply yes it compiles now that i have implemented compareTo method in cd class however theres nothing inside that method and it gives me this message unchecked method invocation i was wondering what would i need to put inside the compareTo method to get Collections.sort() to sort the arraylist in alphabetical order thanks |
|
|
So would you not get penalized if you rewrote that to not use Generics and compiled it under Java 1.4? You wouldn't get a warning for the cast then. If that's the case then the marking system is stupid. In real life the warnings are there to point out that you might have a problem. Penalties should only apply to real ... |
|
Hi, I'm writing a little Applet involving the population of ArrayList (my Java compiler is version 1.6.0_22). My code is similar to ArrayList vertexPoints = new ArrayList(); - it compiles OK, but when I try and run the applet, it falls out saying that there is a NullPointerException in the above line. I was under the impression that the use of ... |