gpt4 book ai didi

java - 在 android 的 HashMap 中使用自定义类

转载 作者:行者123 更新时间:2023-11-30 09:10:55 27 4
gpt4 key购买 nike

编辑:我之前为 java 发布了它。但它在 Java 中工作但在 android 中不工作。所以我现在发布 android 代码。

我正在尝试在我的 Android 应用程序中将自定义类用于 HashMap。但它没有给出我想要的输出。请帮忙。

我想做这样的事情......

//安卓应用代码:

//点类

class Point{
private int x;
private int y;

public Point(){

}
public Point(int _x, int _y){
this.x = _x;
this.y = _y;
}

}

数据管理器.java

public class DataManager {

private Vector<Integer> RSSI = null;
private int numOfRSSI;

private Map<Vector<Integer>, Point> SingleData = null;
private Vector<Map<Vector<Integer>, Point>> Data = null;
private Context context;

public DataManager(Context _context) {
this.context = _context;
Data = new Vector<Map<Vector<Integer>, Point>>();
}

public void loadData(String filename) {
if (Data == null) {
System.out.println("***ERROR: DataSet not initalized!!!\n");
}
readFile(filename);
}

public void printData(){
Vector<Integer> rssi = null;
Map<Vector<Integer>, Point> single = null;
Point point = null;

for(int i=0;i<Data.size();i++){
single = new HashMap<Vector<Integer>, Point>() ;
single = Data.get(0);

for (Map.Entry<Vector<Integer>, Point> entry :single.entrySet()) {

rssi = new Vector<Integer>();
point = new Point();
rssi = entry.getKey();
point = entry.getValue();
System.out.print("("+point.x+" "+point.y+") ");

for(int j = 0;j<rssi.size(); j++){
System.out.print(rssi.get(j)+" ");
}
System.out.println("");
}
}
}

private void readFile(String filename) {

InputStream is = null;
try {
is = context.getResources().getAssets().open("datasets.txt");
} catch (IOException e) {
e.printStackTrace();
System.out.println("error file reading");
}

if (is != null) {

StringBuilder text = new StringBuilder();
int flag = 0;

try {
BufferedReader br = new BufferedReader(
new InputStreamReader(is));

String line;

while ((line = br.readLine()) != null) {

if(flag==0){
flag=1;
this.numOfRSSI = Integer.parseInt(line);
System.out.println("number of RSSI: "+numOfRSSI);
}
else if(flag==1){
parseLine(line);
}

}

} catch (IOException e) {
// Error reading file
}

finally {
// myHelper.print(text.toString());

}
}

}

private void parseLine(String line) {

RSSI = new Vector<Integer>();
SingleData = new HashMap<Vector<Integer>, Point>();


StringTokenizer st = new StringTokenizer(line, ",");
int co = 0;
int x=0,y=0;
while (st.hasMoreTokens()) {

if(co < this.numOfRSSI){
RSSI.add(Integer.parseInt(st.nextToken()));
co++;
}
else{
x = Integer.parseInt(st.nextToken());
y = Integer.parseInt(st.nextToken());
}

}

Point point = new Point(x,y);

SingleData.put(RSSI, point);

Data.add(SingleData);
}

}

主 Activity .java

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

DataManager dataManager = new DataManager(MainActivity.this);

dataManager.loadData("datasets.txt");

dataManager.printData();
}

}

数据集.txt

5-61,-51,-46,-41,-28,1,0

-60,-50,-51,-47,-34,2,0

-72,-52,-53,-55,-37,3,0

-60,-44,-58,-53,-40,3,1

-68,-55,-46,-47,-45,2,1

-66,-60,-48,-43,-37,1,1

-62,-57,-49,-45,-34,0,2

输出显示.....

1 0 -61 -51 -46 -41-28

1 0 -60 -50 -51 -47 -34

1 0 -72 -52 -53 -55 -37

等.....

但应该是……

1 0 -61 -51 -46 -41-28

2 0 -60 -50 -51 -47 -34

等...

所以这是我的问题。

最佳答案

以下在我的机器上运行良好:-

import java.util.Map;
import java.util.Vector;

public class Demo {

public static void main(String[] args) {
Map<Vector<Integer>, Point> vectorPointMap = new HashMap<Vector<Integer>, Point>();

Vector<Integer> vector1 = new Vector<Integer>();
vector1.add(1);
vector1.add(2);
vector1.add(3);

vectorPointMap.put(vector1, new Point(10, 10));

Vector<Integer> vector2 = new Vector<Integer>();
vector2.add(4);
vector2.add(5);
vector2.add(6);

vectorPointMap.put(vector2, new Point(20, 20));

// Print data
for (Map.Entry<Vector<Integer>, Point> entry : vectorPointMap.entrySet()) {


Vector<Integer> keyVector = entry.getKey();
Point valuePoint = entry.getValue();

System.out.print("(" + valuePoint.x + " " + valuePoint.y + ") ");

for (int j = 0; j < keyVector.size(); j++) {
System.out.print(keyVector.get(j) + " ");
}

System.out.println("");
}
}
}

关于java - 在 android 的 HashMap 中使用自定义类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22317881/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com