質問編集履歴
1
コードの追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,3 +1,112 @@
|
|
1
1
|
配列に入れた100個の整数の合計値を出力するプログラムがあります。
|
2
2
|
これに1秒ごとに乱数で出力した整数を1つ追加し、一番古い整数は削除するという機能を追加したいのですが、
|
3
|
-
どのようにすれば実現できますか?
|
3
|
+
どのようにすれば実現できますか?
|
4
|
+
一秒ごとに何かを行う、と言うところまでは実装しましたが、データの移し変え?の方法が分かりません。
|
5
|
+
|
6
|
+
環境はandroid studioです。
|
7
|
+
|
8
|
+
import android.annotation.TargetApi;
|
9
|
+
import android.os.Build;
|
10
|
+
import android.os.Bundle;
|
11
|
+
import android.os.Handler;
|
12
|
+
import android.os.Message;
|
13
|
+
import android.support.v7.app.AppCompatActivity;
|
14
|
+
import android.widget.TextView;
|
15
|
+
|
16
|
+
import java.util.Arrays;
|
17
|
+
|
18
|
+
public class MainActivity extends AppCompatActivity implements Runnable{
|
19
|
+
|
20
|
+
int Date[]=new int[100];
|
21
|
+
int i;
|
22
|
+
int sum=0 ;
|
23
|
+
Double ans;
|
24
|
+
|
25
|
+
public static String testtext;
|
26
|
+
private Thread mThread;
|
27
|
+
public static Handler mHandler;
|
28
|
+
private TextView mTextView;
|
29
|
+
public TextView Testtext;
|
30
|
+
public TextView Testtext2;
|
31
|
+
public static long count = 0;
|
32
|
+
|
33
|
+
|
34
|
+
int timing=1000;
|
35
|
+
|
36
|
+
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
|
37
|
+
@Override
|
38
|
+
protected void onCreate(Bundle savedInstanceState) {
|
39
|
+
super.onCreate(savedInstanceState);
|
40
|
+
setContentView(R.layout.activity_main);
|
41
|
+
|
42
|
+
for(i=0;i<100;i++){
|
43
|
+
Date[i]= (int)( (Math.random()*100));
|
44
|
+
sum+=Date[i];
|
45
|
+
}
|
46
|
+
ans=(double)(sum/100);
|
47
|
+
|
48
|
+
mTextView = (TextView)findViewById(R.id.textView);
|
49
|
+
Testtext = (TextView)findViewById(R.id.textView2);
|
50
|
+
Testtext2 = (TextView)findViewById(R.id.textView3);
|
51
|
+
|
52
|
+
Testtext.setText(Arrays.toString(Date));
|
53
|
+
Testtext2.setText(String.valueOf(ans));
|
54
|
+
mHandler = new Handler() {
|
55
|
+
//メッセージ受信
|
56
|
+
public void handleMessage(android.os.Message message) {
|
57
|
+
//メッセージの表示
|
58
|
+
String text = (String) message.obj;
|
59
|
+
if(message.what==0) {
|
60
|
+
mTextView.setText(text);
|
61
|
+
}
|
62
|
+
if(message.what==2) {
|
63
|
+
Testtext.setText(text);
|
64
|
+
}
|
65
|
+
|
66
|
+
//メッセージの種類に応じてswitch文で制御すれば
|
67
|
+
//イベント制御に利用可能
|
68
|
+
};
|
69
|
+
|
70
|
+
|
71
|
+
};
|
72
|
+
}
|
73
|
+
|
74
|
+
@Override
|
75
|
+
public void onResume(){
|
76
|
+
super.onResume();
|
77
|
+
mThread = new Thread(this);
|
78
|
+
//スレッド処理を開始
|
79
|
+
if(mThread != null ){
|
80
|
+
mThread.start();
|
81
|
+
}
|
82
|
+
count=0;
|
83
|
+
}
|
84
|
+
@Override
|
85
|
+
public void onPause(){
|
86
|
+
super.onPause();
|
87
|
+
//スレッドを削除
|
88
|
+
mThread = null;
|
89
|
+
}
|
90
|
+
//スレッドによる更新処理
|
91
|
+
public void run() {
|
92
|
+
long time = System.currentTimeMillis();
|
93
|
+
|
94
|
+
while (mThread != null) {
|
95
|
+
long now = System.currentTimeMillis();
|
96
|
+
if(now - time > timing){
|
97
|
+
//Message msg = new Message();
|
98
|
+
//Message msg = Message.obtain();
|
99
|
+
Message msg = mHandler.obtainMessage();
|
100
|
+
msg.obj = new String("ループが"+ count + "回終了しました");
|
101
|
+
//ハンドラへのメッセージ送信
|
102
|
+
mHandler.sendMessage(msg);
|
103
|
+
//スレッドの利用変数を初期化
|
104
|
+
time = now;
|
105
|
+
count++;
|
106
|
+
}
|
107
|
+
}
|
108
|
+
}
|
109
|
+
|
110
|
+
|
111
|
+
|
112
|
+
}
|