我看过比较器和算法,但我对它们没有多大意义.来自java.util.Collections的比较器.所以我选择使用这个:
//return an array in descending order, using set algorithm
public int[] descendSort()
{
int[] tempArray = new int[temps.length];
for (int i = temps.length-1; i <= 0; --i)
{
tempArray[i] = temps[i];
}
return tempArray;
}
Run Code Online (Sandbox Code Playgroud)
我在客户端创建的数组是这样的:
int[] temps1 = new int[]{45, 76, 12, 102, 107, 65, 43, 67, 81, 14};
Run Code Online (Sandbox Code Playgroud)
我的输出结果如下:
The temperatures in descending order is: 0 0 0 0 0 0 0 0 0 0
Run Code Online (Sandbox Code Playgroud)
为什么????
所以这个方法的要点是得到一个高于100的温度数组.这有什么问题?当我在toString中返回它时,它说blazing []不存在.
public int[] above100Degrees()
{
int[] blazing = new int[temps.length];
for( int i = 0; i < temps.length; i++ )
{
if( temps[i] > 100 )
{
blazing[i] = temps[i];
}
}
return blazing;
}
Run Code Online (Sandbox Code Playgroud)
toString方法:
public String toString()
{
String returnString = "The temperature forecast of week " + WEEK + " is logged in as: ";
for( int i = 0; i < temps.length; i++ )
{
returnString += "\t" + temps[i] + "\t";
}
returnString += …Run Code Online (Sandbox Code Playgroud) 所以我知道java规则是在扩展和许多其他应用程序时使用ArrayList <>.典型的阵列无法扩展.我的java课程是初级的,所以我们现在仍在审查数组.尽管我想使用一个arraylist我不能.如何将它存储到我只存储满足条件数组中条件的元素的位置?
public int[] above100Degrees()
{
int[] blazing = new int[temps.length];
for( int i = 0; i < temps.length; i++ )
{
if( temps[i] > 100 )
{
blazing[i] = temps[i];
}
}
return blazing;
}
Run Code Online (Sandbox Code Playgroud)
产量
The temperature above 100 degrees is: 0 0 0 0 0 0 0 103 108 109
Run Code Online (Sandbox Code Playgroud) 我对此作业问题非常困惑.我不明白为什么i和sum的值以这种方式出现.我只是不明白这里算法的概念,有人可以解释一下吗?
int i = 0;
int sum = 0;
for(i=0; i < 5; i++)
{
sum += i;
}
System.out.println(i + "\n" + sum);
Run Code Online (Sandbox Code Playgroud)
输出是:
5
10
----jGRASP: operation complete.
Run Code Online (Sandbox Code Playgroud)