今天我在尝试通过使用数组名称作为循环中的变量来简化我的代码时遇到了一个问题,但是编译器一直告诉我变量(数组名称)是一个字符,而不是一个数组,那么如何我修复这个来实现我的问题标题? p.s 我知道下面的代码还有很多问题需要解决,但我只想举一个例子 在循环中使用数组名称作为变量:
(compare[i+1])[i] > (compare[i])[i]
class ArraySort{
public static void main(String[] args){
int[] a = {4, 5, 3};
int[] b = {7, 5};
int[] c = {7, 8, 9};
int[] d = {4, 9, 9};
int[] e = {5, 1};
int[] f = {3, 8, 2, 5};
System.out.println("Before sort: 453 75 789 499 51 3825");
System.out.println("After sort:");
char[] compare = {'a', 'b', 'c', 'd', 'e', 'f'};
char temp;
for(int i = 0; i < 3; i++ ){ // 3 --> max number amount to compare;
for(int j = 0; j < compare.length-1; j++){
if((compare[i+1])[i] > (compare[i])[i]){ // problem is here
temp = compare[i];
compare[i] = compare[i+1];
compare[i+1] = temp;
}
}
}
}
最佳答案
你的代码没有在我的 Java8 环境中编译,我不得不改变一些东西。
首先,数组初始化需要:
int[] a = {4, 5, 3};
int[] b = {7, 5};
int[] c = {7, 8, 9};
int[] d = {4, 9, 9};
int[] e = {5, 1};
int[] f = {3, 8, 2, 5};
然后compare 和temp 对象的init 已更改为数组:
int[][] compare = {a, b, c, d, e, f};
int[] temp;
完整的方法现在看起来是这样的:
class ArraySort
{
public static void main(String[] args)
{
int[] a = {4, 5, 3};
int[] b = {7, 5};
int[] c = {7, 8, 9};
int[] d = {4, 9, 9};
int[] e = {5, 1};
int[] f = {3, 8, 2, 5};
System.out.println("Before sort: 453 75 789 499 51 3825");
System.out.println("After sort:");
int[][] compare = {a, b, c, d, e, f};
int[] temp;
for (int i = 0; i < 3; i++)
{ // 3 --> max number amount to compare;
for (int j = 0; j < compare.length - 1; j++)
{
if ((compare[i + 1])[i] > (compare[i])[i])
{ // problem is here
temp = compare[i];
compare[i] = compare[i + 1];
compare[i + 1] = temp;
}
}
}
}
}
此编译没有任何错误或警告。不能说,如果结果符合预期,因为你的预期结果对我来说不是 100% 清楚。但是提到的编译器问题已经消失。
使用包含变量名的字符或字符串,它在 Java 中并不适用。 有很多方法,当然,您可以使用反射来实现这一点。但我强烈建议在这种情况下不要使用反射。
=== 编辑1 ===
我正在使用 int[][] compare = {a, b, c, d, e, f};
而不是 char[] compare = {'a', ' b', 'c', 'd', 'e', 'f'};
。为什么?
你在代码的开头定义了一些数组 a = {4, 5, 3}
等。所以你有一个变量名为 a
的数组.如果您在字符数组中使用 'a'
(注意 '
),它就是字符 'a'
,它确实没有对存储在变量 a
中的实际数组的任何引用。您也可以选择 'x'
或任何其他字符。
在 char 中使用变量名而不是变量本身需要您手动创建 char 'a'
和变量 a
之间的“链接”。您可以为此使用反射。稍后会详细介绍反射。
通过不使用 char 数组而是使用 int[][]
数组来进行 compare
,您创建了一个数组,它将保存 int[]
“东西”。而您的初始数组 a = {4, 5, 3}
等正是 int[]
的格式。
这使您可以只在比较数组中使用变量本身。
所以在 int[][] compare = {a, b, c, d, e, f};
行中,字符 a 不是 char,而是实际变量,引用您的初始值定义数组。
稍微缩短一下:
int[] a = {4, 5, 3};
int[] b = {7, 5};
int[][] compare = {a, b};
与
相同int[][] compare = {{4, 5, 3}, {7, 5}};
如果你想进行反射(reflection)......你应该去阅读它。这是一个复杂的话题。非常非常非常简单地说,只是为了让您了解:通过反射,您可以在运行时访问(和操作)源代码。
想象一下
int[] a = {1, 2, 3};
char access = 'a';
int[] reflectionOfA = (int[])myClass.getMember(access);
!不是真正的 Java 代码!
myClass.getMember(String)
方法接收一个字符串值,纯文本。然后它会爬取myClass
并搜索一个名为access
值的成员,也就是'a'
。它将找到 int[] 数组 a
并返回它。由于 getMember
方法无法知道搜索到的成员是什么类型(是 int[]、String 还是其他类型?),您需要提供该信息。在这种情况下,它是一个铸件。但我认为实际的 Java Reflections 采用另一个参数,它定义了返回类型。
如果您对反射一无所知,请不要使用它们!阅读有关它们的信息。它们有一个很大的缺点,因为它们的性能不是很好(它们在每次调用时都在对象中爬行,没有缓存,没有任何优化)。
关于java - 在循环中使用数组名称作为变量 (Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52364826/