【问题标题】:Use custom class in HashMap in android在android的HashMap中使用自定义类
【发布时间】:2014-03-11 06:18:02
【问题描述】:

编辑:我之前已经为 java 发布了它。但它可以在 Java 中运行,但不能在 android 中运行。所以我现在发布android代码。

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

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

//android应用代码:

//点类

class Point{
 private int x;
 private int y;

public Point(){

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

}

DataManager.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);
    }

}

MainActivity.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

等等……

所以这是我的问题。

【问题讨论】:

  • 那么,它输出了什么?
  • 对不起。现在我已经添加了输出
  • 对我来说它按预期工作
  • 是的,我同意@VishalSantharam。它应该按预期工作。
  • OK...是的,如果我将它作为 Java 应用程序运行,它就可以工作。但我无法在 android 中得到相同的结果...请稍候,我正在为 android 添加完整代码。

标签: java android hashmap


【解决方案1】:

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

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("");
        }
    }
}

【讨论】:

    【解决方案2】:

    你应该改变这个

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

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

    【讨论】:

      【解决方案3】:

      尝试改变:

      single = Data.get(0);
      

      single = Data.get(i);
      

      【讨论】:

      • 糟糕......这真是一个愚蠢的错误......我的错。现在正常工作
      【解决方案4】:

      “它不给予权利”是什么意思?

      你可以把自己的类放到Map上,没有问题。但是请记住,当您将项目以某种顺序放入 HashMap 时,并不能保证它会以相同的顺序出现。如果您需要在输出上具有与输入相同的顺序,则 HashMap 使用 LinkedHashMap。您可以像使用 HashMap 一样使用它,但它保留了插入项的顺序。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-10-06
        • 2013-05-17
        • 2015-10-14
        • 1970-01-01
        • 2011-10-10
        • 2016-10-24
        • 1970-01-01
        • 2011-07-22
        相关资源
        最近更新 更多