我正在为我的项目研究Kmeans算法。这是KMeans.java:
package com.taffycrash;
import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
/**
* Created by msı on 1.11.2015.
*/
public class KMeans extends Activity {
private ArrayList<Integer> uzayKumesi = new ArrayList<Integer>();
private ArrayList<Integer> kume1 = new ArrayList<Integer>();
private static ArrayList<String> kume_1 = new ArrayList<String>();
private ArrayList<Integer> kume2 = new ArrayList<Integer>();
private static ArrayList<String> kume_2 = new ArrayList<String>();
private ArrayList<Integer> kume3 = new ArrayList<Integer>();
private static ArrayList<String> kume_3 = new ArrayList<String>();
private ArrayList<String> mStringList = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
try {
AssetManager assetManager = getAssets();
InputStream is = assetManager.open("renkler.txt");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
String str = "";
while ((str = bufferedReader.readLine()) != null)
mStringList.add(str); //veriler tek tek okunuyor;
bufferedReader.close();
for (int i = 0; i < mStringList.size(); i++) {
uzayKumesi.add(Integer.parseInt(mStringList.get(i), 16));
}
int merkez1, merkez2, merkez3;
merkez1 = Integer.parseInt("0000CD", 16);
merkez2 = Integer.parseInt("00008B", 16);
merkez3 = Integer.parseInt("000080", 16); //setting random centers for 3 clusters using K-Meaans algorithm
boolean degisim = true;
int temp[] = new int[3];
int eskimerkez1 = 0, eskimerkez2 = 0, eskimerkez3 = 0;
long toplam = 0;
myloop:
while (degisim) {
kume1.clear();
kume2.clear();
kume3.clear();
for (int i = 0; i < uzayKumesi.size(); i++) {
temp[0] = Math.abs(merkez1 - uzayKumesi.get(i));
temp[1] = Math.abs(merkez2 - uzayKumesi.get(i));
temp[2] = Math.abs(merkez3 - uzayKumesi.get(i));
if (temp[0] < temp[1] && temp[0] < temp[2]) kume1.add(uzayKumesi.get(i));
else if (temp[1] < temp[0] && temp[1] < temp[2]) kume2.add(uzayKumesi.get(i));
else if (temp[2] < temp[0] && temp[2] < temp[1]) kume3.add(uzayKumesi.get(i));
}
eskimerkez1 = merkez1;
eskimerkez2 = merkez2;
eskimerkez3 = merkez3;
for (int m = 0; m < kume1.size(); m++) {
toplam += kume1.get(m);
}
if (kume1.size() != 0)
Math.abs(merkez1 = (int) toplam / (kume1.size()));
toplam = 0;
for (int j = 0; j < kume2.size(); j++) {
toplam += kume2.get(j);
}
if (kume2.size() != 0)
Math.abs(merkez2 = (int) toplam / (kume2.size()));
toplam = 0;
for (int k = 0; k < kume3.size(); k++) {
toplam += kume3.get(k);
}
if (kume3.size() != 0)
Math.abs(merkez3 = (int) toplam / (kume3.size()));
if (merkez1 == eskimerkez1 && merkez2 == eskimerkez2 && merkez3 == eskimerkez3) {
degisim = false;
break myloop;
}
if (merkez3 == eskimerkez3 && merkez1 == eskimerkez1 && merkez2 == eskimerkez2) {
degisim = false;
break myloop;
}
if (merkez2 == eskimerkez2 && merkez3 == eskimerkez3 && merkez1 == eskimerkez1) {
degisim = false;
break myloop;
}
}// end while
if (kume1.size() != 0) {
for (int i = 0; i < kume1.size(); i++)
kume_1.add("#" + Integer.toHexString(kume2.get(i)));
}
if (kume2.size() != 0) {
for (int i = 0; i < kume2.size(); i++)
kume_2.add("#" + Integer.toHexString(kume2.get(i)));
}
if (kume3.size() != 0) {
for (int i = 0; i < kume3.size(); i++)
kume_3.add("#" + Integer.toHexString(kume3.get(i)));
}
} catch (IOException e) {
e.printStackTrace();
}
}
我想从另一个扩展View类的类中访问这些特定的ArrayList,它们是kume_1,kume_2和kume_3(不是kume1,kume2,kume3)。这是MyView.java:
package com.taffycrash;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import java.util.ArrayList;
public class MyView extends View {
KMeans kmeans = new KMeans();
ArrayList<Paint> paint= new ArrayList<Paint>();
public MyView(Context context) {
super(context);
init();
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init(){
for(int i =0;i<142;i++) {
paint.add(new Paint());
paint.get(i).setColor(Color.parseColor(kmeans.kume_1.get(0)));
paint.get(i).setStyle(Paint.Style.FILL);
}
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawCircle(50, 50, 20, paint.get(0));
canvas.drawCircle(50, 150, 20, paint.get(1));
canvas.drawCircle(50, 250, 20, paint.get(2));
canvas.drawCircle(50, 350, 20, paint.get(3));
canvas.drawCircle(50, 450, 20, paint.get(4));
canvas.drawCircle(50, 550, 20, paint.get(5)); //for example I want to draw 5 circle
}
}
我想了解从View访问ArrayList的内容。我正在使用kmeans.kume_1.get(0)访问kume_1但它并没有返回我在KMeans.java中首先添加的相同内容。如何访问kume_1在KMeans Class中添加的相同项目。
谢谢。
答案 0 :(得分:0)
如果kume_1是私有的,你如何从MyView获取Kmeans.kume_1?