Swift-在具有多个变量的自定义类数组中查找最大值

时间:2018-11-04 15:06:51

标签: arrays swift

我想在数组中找到最大值。我在Apples文档中找到了.max()方法。

let heights = [67.5, 65.7, 64.3, 61.1, 58.5, 60.3, 64.9]
let greatestHeight = heights.max()
print(greatestHeight)
// Prints "Optional(67.5)"

我的数组具有自定义数据类型。数据类型包含两个来自整数类型的变量。如何从其中一个变量中找到最大值?

这是我的课程。

class Example: Codable {

// Variables
var value1: Int
var value2: Int

// Init
init(_value1: Int, _value2: Int) {
    value1 = _value1
    value2 = _value2
}    

}

3 个答案:

答案 0 :(得分:2)

好吧,class UsersController < ApiController def show @user = User.find(params[:id]) render json: @user, root: 'User', include: ['*', 'subscription', 'user_courses'] end def index @users = User.all render json: @users, root: 'Users', include: ['*' ,'user_courses', 'courses'] end def update @user = User.find(params[:id]) uc = UserCourse.where(user_id: params[:id], course_id: params[:course_id]).first uc.update_attributes(completed_quizzes: params[:completed_quizzes]) if params[:completed_quizzes] uc.update_attributes(completed_lessons: params[:completed_lessons]) if params[:completed_lessons] uc.update_attributes(completed: params[:completed]) if params[:completed] end end实例不是Class,因此您无法访问Array Array,但是可以在其中使用自定义functions获得更大的价值。

类似的东西是一种选择。

func

更新:正如OP所指出的,他只需要比较第一个值。 编辑@Ashley答案,这可以解决

因为class Example: Codable { // Variables var value1: Int var value2: Int // Init init(_value1: Int, _value2: Int) { value1 = _value1 value2 = _value2 } func max() -> Int { if value1 > value2 { return value1 } return value2 } } // USAGE var example = Example(_value1: 10, _value2: 20) example.max() 将返回.max(),其中包含最高的object

Value1

答案 1 :(得分:2)

您可以通过遵循@OnLongClick(R.id.capture) public boolean captureLongPressed() { startRecord(); mMediaRecorder.start(); return true; } @OnTouch(R.id.capture) public boolean captureOnTouch(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_UP) { if (isRecordButtonLongPressed) { mIsRecording = false; try { mMediaRecorder.stop(); mMediaRecorder.reset(); } catch (RuntimeException ex) { } if (listener != null) listener.onVideoRecorded(mVideoFileName); return true; } } return false; } private void startRecord() { try { setupMediaRecorder(); SurfaceTexture surfaceTexture = mTextureView.getSurfaceTexture(); surfaceTexture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight()); Surface previewSurface = new Surface(surfaceTexture); Surface recordSurface = mMediaRecorder.getSurface(); mCaptureRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_RECORD); mCaptureRequestBuilder.addTarget(previewSurface); mCaptureRequestBuilder.addTarget(recordSurface); mCameraDevice.createCaptureSession(Arrays.asList(previewSurface, recordSurface), new CameraCaptureSession.StateCallback() { @Override public void onConfigured(@NonNull CameraCaptureSession session) { try { session.setRepeatingRequest(mCaptureRequestBuilder.build(), null, null); } catch (CameraAccessException e) { e.printStackTrace(); } } @Override public void onConfigureFailed(@NonNull CameraCaptureSession session) { } }, null); } catch (Exception e) { e.printStackTrace(); } } private void setupMediaRecorder() throws IOException { mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE); mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); mMediaRecorder.setOutputFile(mVideoFileName); mMediaRecorder.setVideoEncodingBitRate(1000000); mMediaRecorder.setVideoFrameRate(30); mMediaRecorder.setVideoSize(mVideoSize.getWidth(), mVideoSize.getHeight()); mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264); mMediaRecorder.setOrientationHint(mTotalRotation); mMediaRecorder.prepare(); } @Override public void onPause() { closeCamera(); stopBackgroundThread(); super.onPause(); } private void closeCamera() { if (null != mCameraDevice) { mCameraDevice.close(); mCameraDevice = null; } } 协议并实现Comparable(在这种情况下,我将2个值的总和进行比较)来实现此目的

func <

答案 2 :(得分:1)

正如您在评论中澄清的那样:

  

我想找出该数组中所有对象的value1的最大值

可以通过将每个对象映射到其value1然后确定 最大值:

let maxValue1 = examples.map { $0.value1 }.max()

如果给定的数组真的很大,那么对 使用“惰性变体”来避免创建中间数组:

let maxValue1 = examples.lazy.map { $0.value1 }.max()