如何使两个数组的最大值

时间:2019-03-11 10:19:58

标签: java android arrays

该应用程序有2个数组。它们之间是相互关联的。用户可以选择 数组“国家”的几个项目,然后在第二个活动中显示所选项目的最大位置。例如,用户选择“乌拉圭”,“巴拉圭”,“牙买加”,然后  第二个活动上显示的最大位置(6770000)。如何制作?

package com.example.1.2; 

import android.content.Intent;
import android.media.browse.MediaBrowser;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


public class Main1Activity extends AppCompatActivity implements 
View.OnClickListener {

public static final String KEY_AVERAGE = "average";
String[] countries = {"Urugay", "Paraguay", "Jamaica", "Peru", "Mexico"};
int[] population = {6770000, 2300000, 500000, 6310000, 7000000};
Button btnSubmit;
int sum;
ListView countriesList;
int average;
ArrayAdapter<String> adapter;


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main1);

    Map<String, Integer> countryData = new HashMap<>(countries.length);
    for (int i = 0; i < countries.length; i++) {
        countryData.put(countries[i], population[i]);
    }

    countriesList = findViewById(R.id.countriesList);
    adapter = new ArrayAdapter<String>(this, 
    android.R.layout.simple_list_item_multiple_choice, countries);
    countriesList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    countriesList.setAdapter(adapter);


    btnSubmit = findViewById(R.id.btnSubmit);
    btnSubmit.setOnClickListener(this);
}

  public void onClick(View view)  {

               SparseBooleanArray checked = 
  countriesList.getCheckedItemPositions();
        int max = 0;
        for (int i = 0; i < checked.size(); i++) {
            if (checked.valueAt(i))

//使用户选择最大位置的代码可能在这里

  Intent intent = new Intent(this, Main3Activity.class);
        intent.putExtra(KEY_AVERAGE, average);
        startActivity(intent);
    }
}
public class Main2Activity extends Main1Activity {

TextView tvView;
int average;
@RequiresApi(api = Build.VERSION_CODES.N)
@SuppressLint("SetTextI18n")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    tvView = (TextView) findViewById(R.id.tvView);

    Intent mIntent = getIntent();
    maximum = mIntent.getExtras().getInt(Main1Activity.KEY_MAXIMUM, 
maximum);
    tvView.setText("Maximum: " + maximum);
}
} 

1 个答案:

答案 0 :(得分:0)

首先,您必须存储匹配国家的索引。可以说,用户提交的国家/地区存储在SQL> with test (col) as 2 (select '<pde><a><dov>784512</dov></a></pde>' from dual) 3 select substr(col, instr(col, '<dov>', 1, 1), 4 instr(col, '</dov>', 1, 1) + 6 - instr(col, '<dov>')) result 5 from test; RESULT ----------------- <dov>784512</dov> SQL> 中,这些国家/地区在Array userChoice中的索引存储在“数组matchedIndexes”中。以下代码可以做到这一点:

  1. 存储用户提交的国家/地区的索引。
  2. 比较这些指标上的人口并返回最大人口

代码:

Array countries