Java
和
android
的一些小程序
1
:给定字符串
String[]
str={
"a"
,
"b"
,
"c"
,
"g"
,
"b"
,
"h"
,
"l"
,
"b"
,
"h"
,
"r"
,
"v"
};
,剔除重
复的,并统计出之后其个数
public
class
zhaoshu1 {
//
构造遍历字符串及将重复出现的字符置空
,
显示无重复字符的新字符串及其长度的方法
public
void
checkall(String[] str1) {
int
cnt=0;
for
(
int
i = 0; i
length
-1; i++) {
for
(
int
j = i+1; j
length
; j++) {
if
(str1[i]==str1[j]) {
str1[j]=
""
;
}
}
}
System.
out
.println(
"New string:"
);
for
(
int
i = 0; i
length
; i++) {
if
(!(str1[i].equals(
""
))){
cnt++;
System.
out
.print(str1[i]);
}
}
System.
out
.println();
System.
out
.println(
"The length of new string(str1): "
+cnt);
}
//
注意数组只能用
length
测长度,不能用
size
(),且
length
测出的包含空格
public
static
void
main(String[] args) {
//
TODO
Auto-generated method stub
String[] str={
"a"
,
"b"
,
"c"
,
"g"
,
"b"
,
"h"
,
"l"
,
"b"
,
"h"
,
"r"
,
"v"
};
zhaoshu1 zh=
new
zhaoshu1();
zh.checkall(str);
}
}
2
:一组数,找出其中重复次数最多的数并将其输出
public class findshu2_1 {
public int[] checkfirst(int[] ary1) {
int[] ary2=new int[10];
int[] ary3=new int[10];
System.out.println("
设置
123
为无效数字
");
int cnt=0;
int cnt1=0;
for (int i = 0; i
if (ary1[i]!=123) {
for (int j = i+1; j
if ((ary1[j]!=123)&&ary1[i]==ary1[j]) {
ary1[j]=123;
cnt1++;
}
}
if (cnt1==0) {
ary1[i]=123;
}
else
{
ary2[i]=ary1[i];
ary3[i]=cnt1;
System.out.println(ary1[i]+"
重复
:"+cnt1+"
次
,
即(装载入新数组)
"+ary3[i]+"
次
");
cnt1=0;
}