作者:敏捷的敏2502921017 | 来源:互联网 | 2023-09-06 18:14
我正在尝试在ArrayList中的两个索引之间找到最大值。我对编码非常陌生,经常被卡住。我已经编写了一种方法来查找整个ArrayList的最大值,但是当我尝试编写一种在ArrayList的两个索引之间查找并找到最大值的方法时,我得到的就是整个数组列表的最大值。 / p>
这是在整个ArrayList中找到最大值的方法:
public int findMaxReadingIndex() {
int maxValue = (int) sensorReadings.get(0).getvalue();
int maxIndex = 0;
int i = 0;
for (SensorReading sensorReading : sensorReadings) {
if (sensorReading.getvalue() > maxValue) {
maxValue = (int) sensorReading.getvalue();
maxIndex = i;
}
i++;
}
return maxIndex;
}
这是应该在我的startIndex和endIndex之间找到最大值的方法。在这种情况下,我希望startIndex为5,endIndex为13:
...
public int findMaxReadingIndex1(int startIndex,int endIndex) {
startIndex = (int) sensorReadings.get(5).getvalue();
endIndex = (int) sensorReadings.get(13).getvalue();
int maxValIndex = 0;
for (SensorReading sensorReading : sensorReadings) {
if (startIndex maxValIndex = findMaxReadingIndex();
}
}
return maxValIndex;
}
...
感谢您的帮助!
tl; dr
IntStream // Handy utility method for producing a stream of integers.
.range( 100,120 ) // Generate a stream from 100 to 119. Half-open approach means we go up to,but do not include,the limit of 120.
.boxed() // Convert an `IntStream` to a `Stream`.
.collect( Collectors.toList() ) // Collect the elements from this stream,feeding them into a newly instantiated `List`.
.subList( 5,13 ) // Return a `List` of a subset of elements from the first list.
.stream() // Produce a `Stream` of the elements in that `List`.
.max( Comparator.comparingInt( Integer :: intValue ) ) // Compare each element as an `int`. Perhaps there is a better way to do this comparison,to avoid auto-boxing from object to primitive.
.get() // Extract a value from an `Optional`.
112
流
让我们开始喜欢使用流。
首先,我们的演示需要一系列Integer
对象。
我们可以循环填充这样的List
。
List numbers = new ArrayList <>( 20 );
for ( int i = 100 ; i <120 ; i++ )
{
numbers.add( i );
}
或者在这部分也使用流,作为寻找最大值的墙。
List numbers =
IntStream
.range( 100,120 )
.boxed()
.collect( Collectors.toList() )
;
数字= [100、101、102、103、104、105、106、107、108、109、110、111、112、113、114、115、116、117、118、119]
使用Stream::max
查找最大值。
接下来,我们使用List::subList
从列表中获取所需的元素子集。然后,我们创建该子列表的流。流中的每个元素都通过max
进行馈送,在此处比较整数值作为测试。找到的最大值通过调用Optional::get
返回。
Integer biggest =
numbers
.subList( 5,13 )
.stream()
.max( Comparator.comparingInt( Integer :: intValue ) )
.get()
;
最大= 112
,
类似的事情应该可以解决。
ll in an approximate range (0,2_777_777) with len0=1_111_111
ll in an approximate range (0,2_500_000) with len1=1_000_000
mergeLists: 0.46664454099999997 sec
merge0: 0.2180013780000003 sec
merge1: 0.16215903499999996 sec
ll in an approximate range (0,2_777_777) with len0=1_111_111
ll in an approximate range (-25_000_000,25_000_000) with len1=1_000_000
mergeLists: 0.4489969129999736 sec
merge0: 0.16649408400007815 sec
merge1: 0.1128580580000289 sec
ll in an approximate range (0,22_500_000) with len0=9_000_000
ll in an approximate range (0,22_777_777) with len1=9_111_111
mergeLists: 4.245676940000003 sec
merge0: 1.9906823259999982 sec
merge1: 1.548923153000004 sec
,
好吧,这会让您变得太复杂了,或者我不理解要求。
public int findMaxReadingIndex1(int startIndex,int endIndex)
{
int maxVal = 0;
int maxIndex = 0;
for(int x = startIndex; x <= endIndex; x++)
{
if(sensorReadings.get(x) >= maxVal)
{
maxVal = values[x];
maxIndex = x;
}
}
System.out.println("max value = " + maxVal);
System.out.println("max index = " + maxIndex);
return maxIndex;
}
,
我倾向于认为,像其他答案一样,在这里常规的for循环会更容易,但是要遵循您的方法要说些什么,因为这是您的方法。在我看来,可以稍微调整一下您的原始方法。
public int findMaxReadingIndex1(int startIndex,int endIndex) {
int maxValue = (int) sensorReadings.get(startIndex).getValue();
int maxIndex = startIndex;
int i = 0;
for (SensorReading sensorReading : sensorReadings) {
if (i >= startIndex && i<= endIndex && sensorReading.getValue() > maxValue) {
maxValue = (int) sensorReading.getValue();
maxIndex = i;
}
i++;
}
return maxIndex;
}
然后,如果您不想重复自己,则findMaxReadingIndex()可以使用参数0和sensorReadings.size()-1来调用此方法。