This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 6 years ago.
I have a problem in this method(it is not complete but the problem is on those lines). The thing is that I have a matrix "x", but after going through the absMatriz function, it changes its values. I would like to know how to keep my x matrix, I have tried saving it in another matrix "www" but it does not work.
public Matriz[] problema(Matriz x){
Matriz www=new Matriz(x.m,x.n); //the parameters are the dimensions of the matrix
www= x;
double mm= max(absMatriz(x));
return www;
}
It seems that you're looking to make a deep copy of x before absMatriz mutates it, and return the unmutated deep copy.
See How do I copy an object in Java?
Related
This question already has answers here:
How to sum a list of integers with java streams?
(12 answers)
Is mapToDouble() really necessary for summing a List<Double> with Java 8 streams?
(3 answers)
Closed 1 year ago.
I have a Train class, with a List<TrainCar> containing objects from a TrainCar class.
I have a function in Train called getTotalWeight(). Each TrainCar has a function called getWeight() which returns a value.
Can I use streams in some way to make the body of the getTotalWeight() function looking like this?
return cars.stream().//some function//;
You could use mapToDouble to get a stream of the weights and then sum them:
return cars.stream().mapToDouble(TrainCar::getWeight).sum();
This question already has answers here:
Converting Array to List
(9 answers)
Retrieving a List from a java.util.stream.Stream in Java 8
(15 answers)
Closed 3 years ago.
still very new to Java, my apologies if this has appeared before.
Basically here is the original code
public static MarketType[] convert(final String[] values) {
return ofNullable(values).map(v -> Stream.of(values))
.orElse(Stream.empty())
.map(v -> getMarketType(v))
.toArray(MarketType[]::new);
}
Since other functions changed, I really need the return type to be List<MarketType> instead of MarketType[], but is there any way that can achieve this with the minimal amount of modification for the original code?
I have been trying to put different things in the toArray function but nothing really worked.
Any help appreciated!
This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Assigning in Java?
(5 answers)
Closed 3 years ago.
PS: This question is different than others because I need to know exactly what code to write in order to prevent the variable from changing.
I have this example of code to demonstrate my problem:
float[][] one = {{0}};
float[][] two = one;
System.out.println(two[0][0]);
one[0][0] ++;
System.out.println(two[0][0]);
int three = 0;
int four = three;
System.out.println(four);
three ++;
System.out.println(four);
The output is:
0.0
1.0
0
0
For some reason, when I change float[][] 'one', 'two' automatically changes as well.
However, when I performed the same task but for a different data type int, then the code works as intended.
What is causing this variable to drag the other one with it?
Why doesn't it happen for the int type?
How do I fix it?
This question already has answers here:
Is there a longer than int Java List?
(3 answers)
Using a long as ArrayList index in java
(7 answers)
Closed 7 years ago.
I want to have a big ArrayList, so its index would go beyond int, how or what can I use for that?
I already checked that Vector does have the same issue, any ideas?
This question already has answers here:
Find an array inside another larger array
(15 answers)
Closed 8 years ago.
Is there a way to find an array within another array like
a=[1,2,3,4,5,6,7]
b=[2,3,4]
c=[2,4,5]
// b is child of a, but c is NOT child of a.
Well I know that using Brute-force approach I can find the array within another array. But I want to know that is there any algo that can help me ... or (as I am using JAVA so) is there any built-in feature in JAVA that can help me ?
As already mentioned here :
https://stackoverflow.com/a/3940684/351861:
public static int findArray(Integer[] array, Integer[] subArray)
{
return Collections.indexOfSubList(Arrays.asList(array), Arrays.asList(subArray));
}
Java has builting features for that, apparently.