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))
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 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;
}
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 3 years ago.
Improve this question
So far i tried to add values from a stream to a list with peek() but later I found out that peek() is only used "to support debugging, where you want to see the elements as they flow past a certain point in a pipeline".
Now my question is whats the coding-convention here ?
Do I map it in a second stream or can I map it one String like my Code with Peek() ?
final int range = 9;
List <String> help = new ArrayList<String>();
//random numbers to fill help
for(int i = 5;i< range;i++)
{
help.add(String.valueOf(i+(i*2)+(i*(i+2))) );
}
List<Test> others = new LinkedList<>();
List<Test> tests = help.stream().map(s-> new Test(s,(int) Integer.valueOf("10")))
.peek(t->System.out.println(t.getText()))
.peek(t-> others.add(t)).collect(Collectors.toList());
The class Test looks like this:
public class Test
{
String text;
int id;
public Test(String text, int id) {
this.text = text;
this.id = id;
}
public String getText() {
return text;
}
public int getId() {
return id;
}
}
You could add that functionality to the lambda in the mapping part:
List<Test> tests = help.stream().map(
s-> {
Test t = new Test(s,(int) Integer.valueOf("10")));
System.out.println(t.getText());
others.add(t)
return t;
}
.collect(Collectors.toList());
That simply moves your extra steps into an existing step, avoiding any further loops etc
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 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 8 years ago.
Improve this question
I'm trying to access the DefaultHashMap class but getting error in the main method. Could anyone please tell me what is the problem?
import java.util.Random;
import java.util.*;
public class PythonToJava {
public static void main(String[] args) {
Random rm = new Random();
int i = rm.nextInt(1000);
HashMap<Integer,Integer> stats = new HashMap<Integer,Integer>();
DefaultHashMap<K,V> default = new DefaultHashMap<K,V>();
System.out.println("Random Number Generated is: " + i);
for (int j = 0; j<i; j++){
int value = rm.nextInt(500);
System.out.println("The value of VALUE is " + value);
}
}
}
class DefaultHashMap<K,V> extends HashMap<K,V> {
protected V defaultValue;
public DefaultHashMap(V defaultValue) {
this.defaultValue = defaultValue;
}
#Override
public V get(Object k) {
V v = super.get(k);
return ((v == null) && !this.containsKey(k)) ? this.defaultValue : v;
}
}
Please help me in rectifying the errors I'm encountering at the line with the code:
DefaultHashMap<K,V> default = new DefaultHashMap<K,V>();
The K and V are type parameters, and here, you need to use concrete types to substitute them, the same as when you are using HashMap.
K,V has to be objects
You cannot use variable default. Its a java reserve word.
You should read about java generics
http://docs.oracle.com/javase/tutorial/java/generics/why.html