创建日期:公元2022年02月18日
修改日期:2022-04/28+10/02
文章状态:已完结
平台:Windows7 + eclipse/Bunblebee 2021.1.1版AS
适用于:Java、安卓开发
———————————————————————————————————————————
1.变量类型介绍及使用:
1.0 通用:
变量类型 变量名;
变量类型 变量名 = 赋值;
1.1 字符串String:
//变量名一般为小写,例:
String name;
String sex;
//创建数据库变量时,建议为:
public static final String DATABASE_TABLE_STUDENT = "students";
public static final String KEY_SID = "sid";//sid为自增的Integer类型变量名,直接使用KEY_SID即可
//初始化
String name = "" ;
1.2 整型int/Integer:
int year;
//初始化
int year = 0;
1.3 boolean:
boolean graduated;
//初始化
boolean graduated = false;
1.4 List<Java对象类>与ArrayList:
//Student类:
public class Student {
private String name;
private int year;
public Student(String name, int year) {
this.name = name;
this.year = year;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
}
//如下:初始化了一个关于Student类的List列表
List<Student> stuList = new ArrayList<>();
String name1 = stuList.get(0).getName();//获取stuList的第一个数据的name字段
stuList.get(0).setName("甲");//设置stuList的第一个数据的name字段为——甲
1.5 byte:
一般用byte[]
1.7 blob:
可用于安卓SQLite数据库中存储图片的变量类型
... + KEY_IMG+" blob + ...//数据库中的create语句
Cursor cursor = null;
mDBA.open();
cursor=mDBA.mDB.rawQuery(搜索语句,null);
if (cursor!=null && cursor.moveToFirst()){
//其它参数
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
mDBA.updateRecord(其它参数1,其它参数2,...,baos.toByteArray());//baos.toByteArray()即为传入的blob参数
} else {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
mDBA.insertRecord(其它参数1,其它参数2,...,baos.toByteArray());
}
mDBA.close();
2.相应数组的使用(一维):
2.0 通用:
变量类型[] 变量名 = new 变量类型[该变量的长度];
变量类型[] 变量名 = new 变量类型[]{};
2.1 字符串数组:
//例:
String[] names = new String[]{
"甲", "乙", "丙", "丁"
};
String[] namesx = new String[50];
2.2 整型数组:
//例:
int[] years = new int[]{
10, 11, 12, 10
};
int[] years = new int[50];
3.各变量类型之间的相互转换:
3.1 String && int:
String me = "1314520";
System.out.println("me:" + me);
int a = Integer.parseInt(me);
System.out.println("a:" + a);
int b = a + 9999;
System.out.println("b:" + b);
String you1 = Integer.toString(b);
String you2 = "" + b;
System.out.println("you1:" + you1);
System.out.println("you2:" + you2);
//运行结果:
me:1314520
a:1314520
b:1324519
you1:1324519
you2:1324519
3.2 String && byte[]:
可在网络传输时使用
public void F() throws UnsupportedEncodingException {
String s = "123456";
byte[] bytes = s.getBytes(StandardCharsets.UTF_8);
for (int i = 0; i < bytes.length; i++) {
System.out.println(bytes[i]);
}
String s2 = new String(bytes, "UTF-8");
System.out.println("s2 : " + s2);
}
//运行结果:
49
50
51
52
53
54
s2 : 123456
———————————————————————————————————————————
——原创!转发请注明出处