How to swap two elements (generics) [closed] - java

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

Related

How can I check if an Element in my Lists points to my current element? [closed]

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;
}

How to get field from Object class? [closed]

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;
}

Java 8 Stream | Add a value of the stream to another List/Collection without using foreach or any terminal Operation [closed]

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

Using the Generic Type [closed]

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))

I need to know why this app's output is : 1243 [closed]

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 5 years ago.
Improve this question
class A{
public A(int i) {
System.out.println(1);
}
public A() {
this(10);
System.out.println(2);
}
void A() {
A(10);
System.out.println(3);
}
void A(int i) {
System.out.println(4);
}
}
public class MainClass{
public static void main(String[] args) {
new A().A();
}
}
I don't understand the output of this code. I am a beginner and I really want to know why is this happening, the execution track or what happens when this code runs?
You create new instance of A class with empty constructor. new A()
In empty constructor, first call is the constructor with the value this(10).
Int the constructor with int value you display "1". System.out.println(1)
Then it returns back to empty constructor and call "2". System.out.println(2)
Next, you call the method with no parameters in it. .A()
And it call the the method with parameters. A(10)
Then it prints "4". System.out.println(4)
Also returns back to previous method and print "3" as last operation. System.out.println(3)
https://i.imgur.com/i5l8kTA.png
Sorry for my English.

Categories

Resources