将C++函数翻译成Java
Translate C++ function to Java
首先,如果问题是基本的,我很抱歉,但我不是C++专家。
我正在研究Java中的遗传算法,我到达了这个链接,其中包含有趣的信息:http://web.archive.org/web/20100216182958/http://fog.neopages.org/helloworldgeneticalgorithms.php
然而,我完全不明白这种方法在做什么:
int fitness(bool* chromosome)
{
// the ingredients are: 0 1 2 3 4 5 6
// salt sugar lemon egg water onion apple
return ( -chromosome[0] + chromosome[1] + chromosome[2]
-chromosome[3] + chromosome[4] - chromosome[5]
-chromosome[6] );
}
出于学术目的,我试图将C++程序"翻译"成Java,但我不理解这种方法,到底返回了什么?(我假设它是用数组操作的。)
它返回一个整数。布尔值在相加/相减之前会被转换为整数。True为1。False为0。
这是Java翻译。在我们的例子中,我们必须自己将布尔值转换为整数。
int fitness(boolean[] chromosome)
{
int[] intChromosome = toInt(chromosome);
// the ingredients are: 0 1 2 3 4 5 6
// salt sugar lemon egg water onion apple
return ( -intChromosome [0] + intChromosome [1] + intChromosome [2]
-intChromosome [3] + intChromosome [4] - intChromosome [5]
-intChromosome [6] );
}
int[] toInt(boolean[] values) {
int[] integers = new int[values.length];
for (int i = 0; i < values.length; i++) {
integers[i] = values[i] ? 1 : 0;
}
return integers;
}
相关文章:
- 不同翻译单元中不可重载的非内联函数定义
- 在java中解决这段代码时面临循环中的问题
- 尝试用java代码编译和运行c++代码
- 在这种情况下,java对象是否可以调用本机函数
- 在java中读取c++字节的位字段
- 为什么C++对链表中的下一个节点使用指针,而像 C# 或 Java 这样的语言只使用类 Node 的名称?
- 使用已使用 java 编码的 openssl 解码数据
- 需要将一行代码从C++ Qt翻译成PyQt
- SWIG Java 在使用 -DSWIGWORDSIZE64 时将int64_t转换为 jlong
- 为什么未命名的结构内联变量在每个翻译单元中没有相同的地址?
- C/C++ 到 Java 递归反转链表的翻译
- 这段代码到OpenCV Java的翻译是什么?
- C++ 代码翻译成 Java
- 将Java代码翻译成C++
- 有人可以帮我用java翻译这行代码吗?
- 将C++函数翻译成Java
- c++到Java的翻译
- c++到Java代码的翻译
- Java的翻译过程是什么
- 需要c++到Java翻译的帮助