Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
List<Object> pairs1 = new ArrayList<Object>();
Pair p =new Pair();
p.date="2014-10-10 10:10:10";
p.no=11;
pairs1.add(p);
I created a list like this; now i want to sort it using date without using Collections.sort
Or
Show me how to get only date from list object
If you really want to keep the date implemented as a string and assuming Pair is a class you wrote yourself:
let it implement Comparable
write a suitable implementation of compareTo(Object o)
use Collections.sort(pairs1)
Two ways you can do:
Implement Comparable for your Pair class and implement compareTo method by comparing two dates and then call Collections.sort(pairs1);
Create another class which implements Comparator and implement compare method and then call Collections.sort(pairs1, mycomparator);
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Is it in Java possible to store classes...
Class1 class1 = new Class1();
...in Arrays/ArrayLists, somehow like this:
ArrayList<> classArray = new ArrayList();
classArray.add(class1);
Obviously this doesn't work, but how could it work?
Of course you can. You just specify the type of objects you want to store when you create the array list:
ArrayList<Class1> classArray = ArrayList<Class1>();
classArray.add(class1);
If the class can only be Class1
ArrayList<Class1> list = new ArrayList<Class1>();
Above code won't allow any other class objects it can only store objects of type Class1
If you want to store any class object
ArrayList<Object> list = new ArrayList<Object>();
Object is a parent class of everything in java which is the reason why it can store object of any class
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I think it is better and faster to explain by one example, see the example below:
I have two constants: MY_PREFIX_CONSTANT1 and MY_PREFIX_CONSTANT2.
Then one method that puts those two constants in one array.
Then I add one third constant called: MY_PREFIX_CONSTANT3 but I forgot to add this third constant to the array in this method.
What I want to run is something like a check by which I can notify that the MY_PREFIX_CONSTANT3 constant was not added to the array.
Is this possible?
I would suggest using an enum:
public enum MyEnum {
VALUE1(1),
VALUE2(2),
VALUE3(3);
private final int id;
MyEnum(int id) {
this.id = id;
}
}
You can access all enum values as array using MyEnum.values()
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a list<something> which passed to a method but the method expects like List<Iterator<something>>.
How could i achieve this?
Thanks
Your question is like asking
My client wants apples but I only have oranges. How can I turn my oranges into apples?
If the method requires a list of Iterator then give it a list of iterators! Don't give it a list of objects!
If you have this problem, you must be using the method in the wrong way. But you didn't tell me what the method is. So I can't tell you how to use the method in the correct way.
But anyway, if you really want to do this, here's how
//Assume your list is called "list"
List<Iterator> newList = new ArrayList<>();
for (Object item : list) {
newList.add((Iterator)item);
}
Note: this might result in ClassCastException.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I want to know whats the difference between creating an object[] objects and List<object> objects?
For example lets say I have a class for a book and i wanted to create an object array of books what would the best approach be?
Simply they are two differenct structures.
Object[] obj = new Object[4]; // Creates an array of 4 Object
List<Object> obj = new ArrayList<Object>(); // Creates a list
// (ArrayList in this case) of Object
An array is a structure that can't change its size.
A list can change it.
A list has many different implementations:
ArrayList
LinkedList
Vector
other implementations also custom implementations
An array is a predefined data structure that can't be changed.
A list has many predefined functions not present in an array.
With a list, you do not have to give a specific number of Items, and you can add whenever you want.
If you know exactly how many book instances you need, you can go for an array. But I prefer to use Lists, since it's more comfortable (imo)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Friends
Im posting my question again as previous question was unclear .
I want to pass an object of an class who's member variables are string , Listof A String and a List of an class member variable to a method that copies the contents of above class objects and returns a Lsit of class object which can be used by an java method.
Any example will be nice
Now I have a
Where is the class for CallFlowResource??
A simple way would be for you to create a list and add the values as you want them to be outputted.
It you would look like this:
public List<CallFlowResource> getCallFlow(CallFlowObject obj)
{
List<CallFlowResource> callFlowRes = new ArrayList<>();
for(int i = 0; i<size; i++)
{
callFlowRes.add(obj.GetterMethodForCFR(i));
}
return callFlowRes;
}