标签:use activity utf-8 print 复习 ber stack files 文件读取
课程:《程序设计与数据结构》
班级: 1823
姓名: 赵沛凝
学号:20182301
实验教师:王志强
实验日期:2019年10月23日
必修/选修: 必修
在android上实现实验(1)和(2)
在android平台上实现实验(3)
首先,将一个字符串,进行分割,代码如下:
String[] arr = str.split(","); // 用,分割
System.out.println(Arrays.toString(arr)); // [0, 1, 2, 3, 4, 5]
NumNode head = new NumNode(Integer.valueOf(arr[0]));
int[] intss=new int[20];
intss=StringToInt(arr);
for(int i=1;i<10;i++){
NumNode x = new NumNode(intss[i]);
AddTrail(head,x);
}
然后将字符转为数字,代码如下:
public static int[] StringToInt(String[] arrs){
int[] ints = new int[arrs.length];
for(int i=0;i<arrs.length;i++){
ints[i] = Integer.parseInt(arrs[i]);
}
File file = new File("D:/File/Shuzi.txt");
FileReader fileReader = new FileReader(file);
BufferedReader R =new BufferedReader(fileReader);
String p ="";
p= R.readLine();
fileReader.close();
StringTokenizer tokenizer = new StringTokenizer(p);
int[] number = new int[tokenizer.countTokens()];
int y = 0;
while(tokenizer.hasMoreTokens()){
number[y] = Integer.parseInt(tokenizer.nextToken());
y++;
}
int m = number[0];
NumNode m1 = new NumNode(m);
int n = number[1];
NumNode n1 = new NumNode(n);
public static void AddShu(int x, NumNode element,NumNode head)
{
NumNode temp = head;
if (x == 0)
{
element.next = head;
head = element;
}
else {
for (int y = 1; y < x - 1; y++) {
temp = temp.next;
}
element.next = temp.next;
temp.next = element;
}
}
public static NumNode Delete(NumNode head, NumNode node) {
NumNode current = head, prev = head;
if(current.num==node.num){
head=current.next;}
while (current != null) {
if (current.num != node.num) {
prev = current;
current = current.next;
}
else {
break;
}
}
prev.next = current.next;
if (current.num != node.num)
System.out.println("找不到节点,删除失败。");
return head;
}
public static NumNode AddHead(NumNode head,NumNode node)//头插法为什么有返回值?因为head改了
{
//头插法:在头部插入节点
node.next = head;
head = node;
return head;
}
第三个相较于第二个改动较少,只需要添加一个排序方法,代码如下:
public static void Select(NumNode head)
{
NumNode current = head;
int temp;
while (current != null)//现在的指针
{
NumNode numNode = current.next;//下一个
while (numNode != null)
{
if (numNode.num < current.num)//如果小于
{
temp = current.num;
current.num = numNode.num;
numNode.num = temp;
}
numNode = numNode.next;//下一个
}
current = current.next;//下一个检验,是否需要调换,直到结束
PrintLink(head);
System.out.println("\n"+"链表的个数为: "+length(head));
}
}
在Andriod中需要将主函数的内容进行修改,放在MainActivity进行操作,修改源代码,能够支持两个界面的传递与接受
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,PrintActivity.class);
//从文件读取数字
InputStream input=getResources().openRawResource(R.raw.test);
BufferedInputStream bufferedInputStream = new BufferedInputStream(input);
byte[] buffer = new byte[1024];
String content = "";
int flag = 0;
while (true){
try {
if (!((flag =bufferedInputStream.read(buffer))!=-1)) break;
} catch (IOException e) {
e.printStackTrace();
}
content += new String(buffer,0,flag);
}
StringTokenizer tokenizer = new StringTokenizer(content);
int[] number = new int[tokenizer.countTokens()];
int y = 0;
while(tokenizer.hasMoreTokens()){
number[y] = Integer.parseInt(tokenizer.nextToken());
y++;
}
int m = number[0];
Size.NumNode m1 = new Size.NumNode(m);
int n = number[1];
Size.NumNode n1 = new Size.NumNode(n);
String[] arr = str.split(","); // 用,分割
System.out.println(Arrays.toString(arr)); // [0, 1, 2, 3, 4, 5]
Size.NumNode head = new Size.NumNode(Integer.valueOf(arr[0]));
int[] intss=new int[20];
intss=StringToInt(arr);
for(int i=1;i<10;i++){
Size.NumNode x = new Size.NumNode(intss[i]);
AddTrail(head,x);
}
int nzpn=length(head);
intent.putExtra("username",nzpn);
intent.putExtra("count", PrintLink(head));
//System.out.println("在第五位插入文件中第一个数");
AddShu(5,m1,head);
intent.putExtra("count1", PrintLink(head));
//System.out.println("在第0位插入第二个数");
head=AddHead(head,n1);
intent.putExtra("count2", PrintLink(head));
//System.out.println("从链表中删除刚才的数字1");
Delete(head,m1);
intent.putExtra("delect", PrintLink(head));
Select(head);
intent.putExtra("select", PrintLink(head));
startActivity(intent);
}
});
}
第五个相较于第四个没有太大的区别,只需添加一个Select方法进行排序,添加传递和接受
android.useAndroidX=true
android.enableJetifier=true
android.useAndroidX=false
android.enableJetifier=false
public void deleteNodeByIndex(int index) {
if (index < 1 || index > getListLength()) {
System.out.println("删除位置不合法");}
Node temp = head;
int length = 1;
while (temp.next != null) {
if (index == length) {
temp.next = temp.next.next;
return;}
else {
temp = temp.next;
}
length++;
}
}
btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
dis.setText(readFileSdcardFile(FILE_NAME));
dis.setText(readSDFile(FILE_NAME));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
public String readSDFile(String fileName) throws IOException {
File file = new File(fileName);
FileInputStream fis = new FileInputStream(file);
int length = fis.available();
byte [] buffer = new byte[length];
fis.read(buffer);
String res = EncodingUtils.getString(buffer, "UTF-8");
fis.close();
return res;
}?
public String readFileSdcardFile(String fileName) throws IOException{
String res="";
try{
FileInputStream fin = new FileInputStream(fileName);
int length = fin.available();
byte [] buffer = new byte[length];
fin.read(buffer);
res = EncodingUtils.getString(buffer, "UTF-8");
fin.close();
}
catch(Exception e){
e.printStackTrace();
}
return res;
}
本次实验从idea转战android,我需要把Android理解透彻,才能为以后的APP开发打下更好的基础。
20182301 2019-2020-1 《数据结构与面向对象程序设计》实验6报告
标签:use activity utf-8 print 复习 ber stack files 文件读取
原文地址:https://www.cnblogs.com/zhaopeining/p/11768733.html