避免使用负数组索引

时间:2019-07-14 02:45:34

标签: java arrays android-date array-indexing

这种情况下是否存在可以有效解决以下问题的机制?即,在MainActivity中按下按钮后,我转到SecondActivity,输入数据,然后在SecondActivity中按下按钮并返回MainActivity。

我检索数据,将其分配给变量,然后创建一个放置在数组中的对象。

我声明并初始化了静态变量index = 0,它也负责 日期更改和数组索引。

我创建了两个方法String imageButtonUp()和String imageButtonDown()。 当您按下更改日期的按钮时,将调用这些方法。 如果按下ButtonUp按钮,则变量索引将增加1,然后 将显示下一个日期,而如果按下ButtonDown按钮,则静态变量将减少1,则将显示上一个日期。

问题是,启动应用程序后,一切正常,直到... 当我按下ButtonUp按钮时,索引变量将增加1,因此第二天将出现,并且数组索引将增加为正值。

当我按下ButtonDown时会出现问题,因为变量索引变为负数,这使得 为表的索引分配负值,并且应用程序自动关闭。

一种解决方案是创建一个静态变量,该变量将负责数组的索引。 我可以初始化此变量index = 1000,并创建一个包含2,000个元素的数组。 因此,按下ButtonDown不会收到错误消息,因为索引将为999、998、997 ...

还有其他更有效的解决方案吗?

这是我的代码:

MainActivity:

public class MainActivity extends AppCompatActivity {

    TextView textView, textView2, textView3;
    ImageButton imageButtonDown, imageButtonUp;
    Button button;
    private static final int REQUEST_CODE = 1;

    Date_Information Up_and_Down = new Date_Information("Day", 0);
    Date_Information array[] = new Date_Information[500];

    String day;
    int deegres;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        textView = findViewById(R.id.textView);
        textView2 = findViewById(R.id.textView2);
        textView3 = findViewById(R.id.textView3);
        imageButtonDown = findViewById(R.id.imageButton1);
        imageButtonUp = findViewById(R.id.imageButton2);
        button = findViewById(R.id.button2);


        array[0] = Up_and_Down;
        Up_and_Down.setDate();

        textView.setText(Up_and_Down.setDate());



        imageButtonDown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            textView.setText(Up_and_Down.imageButtonDown());

            if(array[Date_Information.index] == null) {

                textView2.setText("Day:");
                textView3.setText("Deegres Celsius:");
            }

            else {

                textView2.setText(array[Date_Information.index].getDay());
                textView3.setText(array[Date_Information.index].getDegrees_Celsius());

            }

            }
        });

        imageButtonUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                textView.setText(Up_and_Down.imageButtonUp());

                if(array[Date_Information.index] == null) {

                    textView2.setText("Day:");
                    textView3.setText("Deegres Celsius:");
                }

                else {

                    textView2.setText(array[Date_Information.index].getDay());
                    textView3.setText(array[Date_Information.index].getDegrees_Celsius());
                }



            }
        });

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent i = new Intent(MainActivity.this, SecondActivity.class);
                startActivityForResult(i, REQUEST_CODE);
            }
        });



    }

    protected void onActivityResult(int requestCode, int resultCode, Intent i) {
        if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {

            day = i.getStringExtra("day");
            deegres = i.getIntExtra("deegres", 0);

            Date_Information test = new Date_Information(day, deegres);
            array[Date_Information.index] = test;

            textView2.setText(array[Date_Information.index].getDay());
            textView3.setText(array[Date_Information.index].getDegrees_Celsius());

        }
    }

}

SecondActivity:

public class SecondActivity extends AppCompatActivity {

    EditText editText1, editText2;
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        editText1 = findViewById(R.id.editText);
        editText2 = findViewById(R.id.editText2);
        button = findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                sendInformation();
            }
        });

    }

    public void sendInformation() {

        String day = editText1.getText().toString();
        int deegres = Integer.parseInt(editText2.getText().toString());

        Intent i = new Intent();
        i.putExtra("day", day);
        i.putExtra("deegres", deegres);

        setResult(RESULT_OK, i);
        finish();
    }
}

和Date_Information类:

public class Date_Information {

    Calendar calendar; //The reference variable that will store the Calendar instance
    SimpleDateFormat dateFormat ; //Reference variable that will refer to the object that is responsible for the date format
    String formattedDate; //Formatted date
    private String day; //Day
    private int degrees_Celsius; //Deegres C
    static int index = 0;

    //The parametrised constructor, which sets the date format, assigns the sent day and the component step of the object
    Date_Information(String day, int degrees_Celsius) {

        dateFormat = new SimpleDateFormat("dd - MMM - yyyy");
        this.day = day;
        this.degrees_Celsius = degrees_Celsius;
    }

    Date_Information() {

        dateFormat = new SimpleDateFormat("dd - MMM - yyyy");
    }
    //The method returns the formatted date
    String setDate() {

        calendar = Calendar.getInstance(); //Get the instance and assigning it to the reference variable calendar

        formattedDate = dateFormat.format(calendar.getTime()); //Formatting the downloaded date and assigning to the variable formattedDate
        return formattedDate;
    }

    //A method that increases the date by 1 day
     String imageButtonUp() {

         index++;
         calendar = Calendar.getInstance();
         calendar.add(Calendar.DATE, index);
         formattedDate = dateFormat.format(calendar.getTime());

         return formattedDate;

     }

     //
     //Method that reduces the date by 1 day
    String imageButtonDown() {

            index --;
            calendar = Calendar.getInstance();
            calendar.add(Calendar.DATE, index);
            formattedDate = dateFormat.format(calendar.getTime());

            return formattedDate;
        }

    String getDay() {

        return day;
    }


    String getDegrees_Celsius() {

        return String.valueOf(degrees_Celsius);
    }
}

0 个答案:

没有答案