I have a class with two ArrayLists that store two different types of objects
I have another class - SortedArrayList - which is responsible for sorting the above arraylists alphabetically and printing them to the console or to the file
I was wondering if there is any way for the SortedArrayList to acquire the added objects from the previous ArrayLists without adding them again to the SortedArrayList?
Please point me in the right direction
Thank you
If the objects are similar, you can use an interface so that the SortedArrayList can work with that and then will be able to work with both Objects.
Related
I am new to OOP, and still trying to wrap my head around just how encapsulated things should be. This question is about best practices, NOT about how to achieve functionality.
For an assignment, we are asked to make linked lists whose nodes contain two Strings: the name of the person spreading a disease, and the name of the person becoming infected. Each case of infection is only a record of who is involved, an infection doesn't actually do anything.
The assignment description suggests we add the two names as fields to the Nodes of the linked list. But my fledgling OOP-radar is booping, and I am unsure of whether or not I should instead create a nested Infection class within the node, or a top-level class of its own, which stores the two Strings.
So my internal conflict (and question) here is: at what point does an object become too simple to merit being an object anymore, while still keeping within the OOP-paradigm? Should I create an Infection class, or add data to the Node to keep it simple?
I would approach it the same way as Collections API does it: Create generic data structures that can hold any kind of objects and let the objects to define their internal structure/functionality.
The type could be generified, that would be the best practice.
I'm trying to make a variable offsets which equals ((2,1),(2,-1),(1,-2)) through which I can iterate and get the X,Y coordinates for each position.
In python, I would just use a list of lists. I thought I would be able to do this in Java, too, but it seems much more difficult to do so with this language...
I though maybe int[] offsets would work, but no dice, apparently. Then, as it's a list of lists (not just a list), I tried int[][] offset and eclipse still thought I was a moron.
What am I missing? Or am I just making this harder than it needs to be, and there's actually some really simple thing you'd suggest instead of a list of lists?
The best idea would be to use Point
Point point = new Point(1,4);
List<Point> points = new ArrayList<Point>();
Another way is to use list of lists. Not recommended. Not object oriented
List<List<Integer>>
Alternatively you can create your version of Point class. For example an object which will have one axis inside
class AxisX{
private List<Integer>
}
And put it inside another object
class Coordinates{
private List<AxisX>
}
from what's written here, it looks like you're trying to access list members with array syntax.
In java, collections and arrays have completely different syntax.
arrays are indexed with [], collections such as lists use methods.
so to get the i'th element of the j'th list
list.get(j).get(i);
the first get call returns the list at the i'th index of list
the second get call, returns the j'th element of the inner list.
though for this case, I'll second #Tom_G and suggest using something like a point object
then you only need one list, the point handling x, y coordinates in a single object.
if you will use this in something related to serialization and remote data transfer, try using the implementer classes of List, and not List itself (while declaring your variable), so not to get stuck in the middle, like
ArrayList<ArrayList<Integer>> ....
because the Collections framework interfaces dont extend the Serializable interface
maybe this helps, becuase i lived a horrible experience because i didnt notice this from the start
I'm working with java and I need some ideas , I have two arraylists lis1 and lis2. The first one contains elements
lis1[a,b,c,d]
and the second one
lis2[a',b',c',d']
If I make a sort to my first list lis1 for example
lis1[b,c,a,d] I want the second one to become lis2[b',c',a',d']. I need some help on it just an idea.
It seems to me that you have one list here, populated with objects each containing a and a', b and b' etc.
e.g. something like List<Pair>
where Pair is an object containing your two elements. You can then write a comparator for Pair which simply compares between the first element of each Pair object.
I wouldn't use two lists here. You have to keep them in sync, and this seems fragile at best.
One way may be, Write your own comparator with required sorting logic. Apply same comparator on both list while sorting.
Possible Duplicate:
Primitive Array vs ArrayList
What is the difference between List and Array in java? or the difference between Array and Vector!
In general (and in Java) an array is a data structure generally consisting of sequential memory storing a collection of objects.
List is an interface in Java, which means that it may have multiple implementations. One of these implementations is ArrayList, which is a class that implements the behavior of the List interface using arrays as the data structure.
There are a number of other classes that implement the List interface. One easy way to take a look at them is by viewing the Javadoc for List: http://docs.oracle.com/javase/6/docs/api/java/util/List.html
On that page, you'll see "all known implementing classes," which are all of the kinds of lists in Java.
I Know I can use Generics while defining the ArrayList to do that. But here the case is different.
I have a ArrayList, which when defined accepts any type of Objects. Once the user inserts the first Object, I need to use Class Reference to Find the Class of that Object and then have to ensure that only Objects of that Class are inserted in the ArrayList.
Example:
ArrayList arrayList = new ArrayList();
Now lets say the User enters an object b, of Class B in the arrayList, then from now onwards, I must only allow objects of type B to be added to the arrayList.
I know that I can find the class of the Object inserted using:
arrayList.get(0).getClass();
But what after it? How will I use the Class type I just found?
Since this is an interview question I won't give you a complete answer, but you might want to take a look at the Class.isAssignableFrom method.
You cannot use generics for this, you need to implement runtime checks.
One way would be to subclass ArrayList and implement the various add methods in a way that checks the type of what is being added.
get(0).getClass().cast(newObject);
// will throw a ClassCastException if it does not match
hmm .. you can do the comparison on the class names - not elegant but should do your work ..
get(0).getClass().getName().equals(classname for the pushed value)
I see some design issues in the code rather how to solve this issue. The flow of the code should determine what the code is doing so that it does not send the collection to a method which can put any arbitrary objects (by checking or not) in it.
I would advise to revisit the design.
For example, if someone is trying to put a soccer ball into the collection and then the collection should be passed into a method where it can deal with a soccer ball. They can use a common code or command pattern for handling any ball or specific behavior for soccer ball and so on.
The same is true if the code wants to put a base ball into a collection, it better knows what its going to do next.
It is a design issue... It is not a code issue.