- Create the Java List: Convert the MATLAB array to a Java ArrayList of integers.
- Pass the List to Java: Call the Java method with the ArrayList.
How to Array Matrix 1-by-8 Pass to java method error:No method with matching signature found for class?
1 回表示 (過去 30 日間)
古いコメントを表示
Hi,
On Matl-lab Part :
matrix =[1,2,3,4,5,6,7,8]; % example 1-by-8 dimension
list=java.util.ArrayList();
for k=1:length(matrix)
list.add(int32(matrix(k)));
end
training = MainMethod;
training.list_method(list.add(int32(matrix(k))));
On Java Part
public void list_method(List<Integer> points){
//Here I have Expect points result like [1,2,3,4,5,6,7,8]
recordedSample.getMatrix().add(points);//Sample Class object
}
Note: From java Code Method arguments replaced List Integer to Double[] (array) also i have got error same .please let me know how i can solve this issues.?
0 件のコメント
回答 (1 件)
Prateekshya
2024 年 10 月 14 日
Hello Sameer,
To pass a MATLAB array to a Java method expecting a List<Integer>, you need to ensure that the data types are compatible and that the Java method is correctly receiving the data. Here are the steps to achieve this:
Here is how you can do it:
% MATLAB code
matrix = [1, 2, 3, 4, 5, 6, 7, 8]; % Example 1-by-8 dimension
list = java.util.ArrayList();
% Add each element of the matrix to the Java ArrayList as Integer
for k = 1:length(matrix)
list.add(java.lang.Integer(matrix(k)));
end
% Assuming MainMethod is a Java class you've imported
training = MainMethod();
training.list_method(list);
Ensure your Java method is set up to receive a List<Integer>:
import java.util.List;
public class MainMethod {
public void list_method(List<Integer> points) {
// Expecting points result like [1,2,3,4,5,6,7,8]
recordedSample.getMatrix().add(points); // Sample Class object
}
}
Ensure that the data types match in both the cases. MATLAB can interact with Java objects and methods, but you must ensure that the Java class is properly compiled and accessible from the MATLAB environment.
I hope this helps!
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Java Package Integration についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!