導航:首頁 > 操作系統 > androidmapput

androidmapput

發布時間:2022-09-14 03:37:03

⑴ 安卓 Map.put(position,null)和Map.remove(position)好在哪裡

Map採用鍵值對方式,通常使用HashMap。其速度快,解析方便。

android map是怎麼排序

java中的Map是個key-valus形式存儲的健值對介面。Map是個介面,因此不能實例化,不同的子類有不同的實現方式。

  1. HashMap 無序的,通過hash演算法來(散列存儲)。

  2. Hashtable 無序的,通過hash演算法來(散列存儲),與HashMap不同的是Hashtable 是同步的(線程安全的)。

  3. LinkedHashMap 有序的,通過雙向鏈表來存儲數據。

  4. TreeMap 排序的,通過Key來排序,排序規則會調用key的compareTo方法來計算。

⑶ android map.keyset 按照什麼順序

有一個Map對象,這時候使用keySet()方法獲取所有的key值,比如: Map map = new HashMap(); map.put(1, "a"); map.put(2, "b"); map.put(3, "c"); map.put(4, "d"); Set keys1 = map.keySet(); Set keys2 = map.keySet(); Set keys3 = map.keySet();上面三個set對象key1,key2,key3引用的是一個對象。這時map的keySet()方法只返回一個set實例,所以當從key1中刪除一個對象時候,key2和key3將會受到影響。 keys1.remove(1); System.out.println(keys1); System.out.println(keys2); System.out.println(keys3);列印結果為:[2, 4, 3][2, 4, 3][2, 4, 3]
下面是摘自API幫助文檔的說明
keySetpublic Set<K> keySet()返回此映射中所包含的鍵的 set 視圖。該集合受映射的支持,所以映射的變化也反映在該集合中,反之亦然。該集合支持元素的移除,通過 Iterator.remove、Set.remove、removeAll、retainAll 和 clear 操作,從該映射中移除相應的映射關系。它不支持 add 或 addAll 操作。
指定者:介面 Map<K,V> 中的 keySet覆蓋:類 AbstractMap<K,V> 中的 keySet返回:此映射所包含的鍵的 set 視圖。

⑷ android中給map賦值,怎麼寫

Map<String, String> map = new HashMap<String, String>();
map.put("sss", "value");
map.put("aaa", "value");
map.put("zzz", "value");
map.put("xxx", "value");

⑸ 在Android中要實現圖表統計該怎麼做

package com.yzxy.draw;

import java.util.ArrayList;
import java.util.HashMap;
import com.yzxy.draw.tools.Tools;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.RectF;
import android.graphics.Paint.Style;
import android.graphics.Typeface;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

@SuppressLint("ViewConstructor")
class MyTuView extends View{

public static final int RECT_SIZE = 10;
private Point mSelectedPoint = null;
public static enum Mstyle
{
Line,scroll
}

private Mstyle mstyle=Mstyle.Line;
private Point[] mPoints = new Point[8];

Context context;
Activity act;
int bheight=0;
Tools tool=new Tools();
HashMap map;
ArrayList dlk;
int totalvalue=30;
int pjvalue=5;
String xstr,ystr;
int margint=15;
int marginb=40;
int c=0;
int resid=0;
Boolean isylineshow;

public MyTuView(Context context,HashMap map,int totalvalue,int pjvalue,String xstr,String ystr,Boolean isylineshow)
{
super(context);
this.context=context;
this.act = (Activity)context;
this.map=map;
this.totalvalue=totalvalue;
this.pjvalue=pjvalue;
this.xstr=xstr;
this.ystr=ystr;
this.isylineshow=isylineshow;
act.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}

@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// canvas.drawColor(Color.GRAY);
if(c!=0)
this.setbg(c);
if(resid!=0)
this.setBackgroundResource(resid);
dlk=tool.getintfrommap(map);
int height=getHeight();
if(bheight==0)
bheight=height-marginb;

int width=getWidth();

Log.i("w", getWidth()+":"+getHeight());
int blwidh=tool.dip2px(context,50);
int pjsize=totalvalue/pjvalue;

Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.GRAY);
paint.setStrokeWidth(1);
paint.setStyle(Style.STROKE);

// 畫直線(橫向)
for(int i=0;i xlist=new ArrayList();//記錄每個x的值
//畫直線(縱向)
for(int i=0;i dlk,HashMap map,ArrayList xlist,int max,int h)
{
Point[] points=new Point[dlk.size()];
for(int i=0;i getMap() {
return map;
}

public void setMap(HashMap map) {
this.map = map;
}

public int getTotalvalue() {
return totalvalue;
}

public void setTotalvalue(int totalvalue) {
this.totalvalue = totalvalue;
}

public int getPjvalue() {
return pjvalue;
}

public void setPjvalue(int pjvalue) {
this.pjvalue = pjvalue;
}

public String getXstr() {
return xstr;
}

public void setXstr(String xstr) {
this.xstr = xstr;
}

public String getYstr() {
return ystr;
}

public void setYstr(String ystr) {
this.ystr = ystr;
}

public int getMargint() {
return margint;
}

public void setMargint(int margint) {
this.margint = margint;
}

public Boolean getIsylineshow() {
return isylineshow;
}

public void setIsylineshow(Boolean isylineshow) {
this.isylineshow = isylineshow;
}

public int getMarginb() {
return marginb;
}

public void setMarginb(int marginb) {
this.marginb = marginb;
}

public Mstyle getMstyle() {
return mstyle;
}

public void setMstyle(Mstyle mstyle) {
this.mstyle = mstyle;
}

public int getBheight() {
return bheight;
}

public void setBheight(int bheight) {
this.bheight = bheight;
}

public int getC() {
return c;
}

public void setC(int c) {
this.c = c;
}

public int getResid() {
return resid;
}

public void setResid(int resid) {
this.resid = resid;
}

}

代碼片段
package com.yzxy.draw.tools;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Environment;

public class Tools
{
private final static String ALBUM_PATH = Environment.getExternalStorageDirectory() + "/yueqiu/";

public int dip2px(Context context, float dpValue)
{
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}

public int px2dip(Context context, float pxValue)
{
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}

public ArrayList getintfrommap(HashMap map)
{
ArrayList dlk=new ArrayList();
int position=0;
@SuppressWarnings("rawtypes")
Set set= map.entrySet();
@SuppressWarnings("rawtypes")
Iterator iterator = set.iterator();

while(iterator.hasNext())
{
@SuppressWarnings("rawtypes")
Map.Entry mapentry = (Map.Entry)iterator.next();
dlk.add((Double)mapentry.getKey());
}
for(int i=0;i

代碼片段
package com.yzxy.draw;

import java.io.IOException;
import java.util.HashMap;

import com.yzxy.draw.MyTuView.Mstyle;
import com.yzxy.draw.tools.Tools;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;

import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.Toast;
import android.app.Activity;
import android.content.res.XmlResourceParser;
import android.graphics.Bitmap;
import android.graphics.Color;

public class MainActivity extends Activity {

MyTuView tu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

HashMap map=new HashMap();
map.put(1.0, 8.0);
map.put(7.0, 130.0);
map.put(2.0, 4.0);
map.put(3.0, 1.0);
map.put(4.0, 18.0);
map.put(5.0, 160.0);
map.put(6.0, 180.0);
map.put(8.1, 133.5);
tu=new MyTuView(this,map,200,50,"x","y",false);
// tu.setC(Color.CYAN);
tu.setResid(R.drawable.bg);
// tu.setBheight(200);
tu.setTotalvalue(200);

tu.setPjvalue(50);
tu.setXstr("x");
tu.setYstr("y");
tu.setMargint(20);
tu.setBackgroundColor(Color.WHITE);
tu.setMarginb(50);
tu.setMstyle(Mstyle.scroll);
RelativeLayout rela=getlayout(R.layout.activity_main);
rela.addView(tu);
LayoutParams parm=new LayoutParams(1200,400);
parm.setMargins(50, 50, 50, 100);
tu.setLayoutParams(parm);
setContentView(rela);

}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
// TODO Auto-generated method stub
if(item.getItemId() == R.id.menu_settings)
{
if (false == tu.isDrawingCacheEnabled())
{
tu.setDrawingCacheEnabled(true);
}
Bitmap bitmap = tu.getDrawingCache();
Tools tool=new Tools();
try {
Boolean b=tool.saveFile(bitmap, "aaaa.png");
if(b)
Toast.makeText(this, "success", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(item.getItemId() == R.id.menu_ch)
{
HashMap map=new HashMap();
map.put(1.0, 21.0);
map.put(3.0, 25.0);
map.put(4.0, 32.0);
map.put(5.0, 31.0);
map.put(6.0, 26.0);

tu.setTotalvalue(40);
tu.setPjvalue(10);
tu.setMap(map);
tu.setIsylineshow(true);
tu.postInvalidate();
}
if(item.getItemId() == R.id.menu_ch2)
{
HashMap map=new HashMap();
map.put(1.0, 41.0);
map.put(3.0, 25.0);
map.put(4.0, 32.0);
map.put(5.0, 41.0);
map.put(6.0, 16.0);
map.put(7.0, 36.0);
map.put(8.0, 26.0);
tu.setTotalvalue(50);
tu.setPjvalue(10);
tu.setMap(map);
tu.setMstyle(Mstyle.Line);
tu.setIsylineshow(false);
tu.postInvalidate();
}
return true;
}

public RelativeLayout getlayout(int r)
{
LayoutInflater inflater = (LayoutInflater)getSystemService(android.content.Context.LAYOUT_INFLATER_SERVICE );
try {
XmlResourceParser parser = getResources().getLayout(r);
RelativeLayout layout = (RelativeLayout) inflater.inflate(parser, null);
return layout;
}catch (Exception e) {
// TODO: handle exception
}
return null;
}
}

⑹ Android開發 HashMap如何排序

HashMap排序是數據結構與演算法中常見的一種排序演算法。本文即以Android平台為例來實現該演算法。
具體代碼如下: public static void main(String[] args) { Map<String, Integer> map = new HashMap<String, Integer>(); map.put("lisi", 5); map.put("lisi1", 1); map.put("lisi2", 3); map.put("lisi3", 9); List<Map.Entry<String, Integer>> infoIds = new ArrayList<Map.Entry<String, Integer>>( map.entrySet()); System.out.println("--------------排序前--------------"); for (int i = 0; i < infoIds.size(); i++) { String id = infoIds.get(i).toString(); System.out.println(id); } // 排序 Collections.sort(infoIds, new Comparator<Map.Entry<String, Integer>>() { public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) { return ( o1.getValue()-o2.getValue()); } }); System.out.println("--------------排序後--------------"); for (int i = 0; i < infoIds.size(); i++) { Entry<String,Integer> ent=infoIds.get(i); System.out.println(ent.getKey()+"="+ent.getValue()); }}

閱讀全文

與androidmapput相關的資料

熱點內容
加好友伺服器繁忙是怎麼回事 瀏覽:379
怎麼解綁app的支付寶賬號 瀏覽:911
ip地址伺服器不可用怎麼解決方法 瀏覽:183
為什麼軟體需要伺服器 瀏覽:63
redis操作命令大全 瀏覽:597
python字元串重復索引 瀏覽:961
為什麼香信新版本連接不上伺服器 瀏覽:50
元旦程序員打羽毛球 瀏覽:614
otc焊接機器人離線編程教學 瀏覽:412
51單片機的ea引腳有何用途 瀏覽:207
centos查看用戶命令 瀏覽:840
程序員臉胖 瀏覽:744
hdfs在主目錄下創建文件夾 瀏覽:800
股票選股器源碼公式如何弄 瀏覽:31
伺服器如何使用在微信上 瀏覽:328
app登不了是怎麼回事 瀏覽:254
dd命令u盤 瀏覽:570
單片機生日快樂程序 瀏覽:893
安卓手機連車載的叫什麼 瀏覽:223
怎麼讓自己的手機鍵盤變得好看app 瀏覽:53