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
I have a object like below:
public void getField(List<?> list) {
int lastIndex = list.size()-1;
Object ob = list.get(lastIndex);
}
How can I return a specific field from this ob like e.q. status?
Possible is the solution for your case:
public <T> T getField(List<T> list) {
int lastIndex = list.size()-1;
T ob = list.get(lastIndex);
return ob;
}
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 7 days ago.
Improve this question
[![enter image description here][1]][1]
Hello, everyone. I am a little bit confused in swapping. Plaese correct my swap method. I have tried to solve it in my ownself
I tried to not use temp
Swap<Integer, String> integerStringSwap = new Swap<>();
----------------------------------------------------
public class Swap<A,B> {
private A first;
private B second;
public Swap(A first, B second) {
this.second=second;
this.first=first;
}
void setFirst(A first){
this.first=first;
}
void setSecond(B second){
this.second=second;
}
A getFirst(){
return first;
}
B getSecond(){
return second;
}
void swap(){
}
}````
[1]: https://i.stack.imgur.com/yN3tu.png
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
I'm working on a programming exercise, and I stumbled across a problem about "comparing" List objects for a method.
Goal: Check if the previous object in the List is null or if its next attributes points at my current object.
My idea was the following:
public ListElement (String info, ListElement next, ListElement prev) {
this.info = info;
this.next = next;
this.prev = prev;
}
public boolean isDeleted() {
ListElement prevElement = getPrev();
ListElement nextElement = getNext();
if(prevElement.next.info.equals(this.info))
return false;
}
However, I'm not sure if I get the correct result / about the approach.
Is there a better way to do this?
Assuming "points to" means "is the exact same instance", you'd have to use == for comparison.
public boolean isDeleted() {
return
// if the previous Object in the List is null
prev == null ||
// if its next attributes points at my current Object
next == this;
}
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 4 years ago.
Improve this question
My Instructor for my Data structures class has told me that there is a better way to implement using a Generic Data type in this method instead of casting everything to E. I am unable to figure out how this better way is implemented or exactly what she means. I know this method I wrote works but if there is a better way I would like to know.
public class GenericSortedArrayBag<E extends Comparable> implements Cloneable,Iterable<E> {
public int numPresents;
public int maxPresents;
private Object[] data;
public void delete(E k) {
boolean found = false;
for(int i=0; i <numPresents; i++) {
if(((E)data[i]).equals(k)) {
found = true;
}
if(found && i<numPresents - 1) {
data[i] = data[i+1];
}
else if(found) {
data[i] = null;
}
}
numPresents--;
}
Instead of
private Object[] data;
you can use
private E[] data;
That way you save the cast
if((data[i]).equals(k))
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
is there a way to say that if you reach the null reference, then do something ?
e.g. if i have a linkedlist with just one object and after this comparison, if you reached null then do something...
for(int i = 0; i < queue.size(); i++) {
if (queue.get(i).compareTo(newitem) == -1) {
continue;
}
}
for instance another if-clause in the loop saying
if (queue.equals(null)) {
queue.add(newitem);
}
this is btw the linkedlist
public OrderedQueue() {
queue = new LinkedList<T>(); // generate an empty queue
}
You can always check that the object in your list is null to do a special operation:
for(Element e : queue) {
if(e == null) {
// special operation
} else {
// normal operation
}
}
Also use a for-each loop to iterate over elements.
In your case, using the get(i) method is not very efficient on a LinkedList. Using a for each allows you to abstract the actual type of the Iterable you are iterating over.
Replace everything with:
if (!queue.contains(newItem)) {
queue.add(newItem);
}
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
I want to implement a Comparator for two Strings representing 2 dates that compares the dates and adds them to a tree TreeSet accordingly.Thanks.
This is one posibility to compare the date after formating the string :
import java.util.Date
public class ComparatorExample {
private static class DateComparator implements Comparator<Date> {
#Override
public int compare(String s1, String s2) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = sdf.parse(s1);
Date date2 = sdf.parse(s2);
System.out.println(sdf.format(date1));
System.out.println(sdf.format(date2));
if(date1.compareTo(date2)>0){
return 1;
}else if(date1.compareTo(date2)<0){
return -1;
}else if(date1.compareTo(date2)==0){
return 0;
}
}
}
}