package com.demo.calculate;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity {
private int[] arr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
arr = new int[]{2,3,4,5,6,7,8,9};
findViewById(R.id.btn_add).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addElementToLast(11);
}
});
findViewById(R.id.btn_insert).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
insertElement(2,5);
}
});
findViewById(R.id.btn_delete).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
deleteElement(2);
}
});
findViewById(R.id.btn_lineSearch).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("tag",lineSearch(2)+"");
}
});
findViewById(R.id.btn_addQueue).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addQueue(10);
}
});
findViewById(R.id.btn_popQueue).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popQueue();
}
});
findViewById(R.id.btn_bubbleSort).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bubbleSort();
}
});
}
/**
* 冒泡排序
*/
private void bubbleSort() {
int[] arr = new int[]{3,2,5,7,4,1,0,8};
for (int i = 0; i <arr.length-1 ; i++) {
for (int j = 0; j <arr.length-1-i ; j++) {
//交换位置
if (arr[j]>arr[j+1]){
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
Log.i("tag",Arrays.toString(arr));
}
/**
* 出队
*/
private void popQueue() {
if (arr.length==0){
return;
}
int[] newArr = new int[arr.length-1];
for (int i = 0; i <newArr.length ; i++) {
newArr[i] = arr[i+1];
}
arr = newArr;
Log.i("tag",Arrays.toString(arr));
}
/**
*入队
*/
private void addQueue(int element) {
int[] newArr = new int[arr.length+1];
for (int i = 0; i <arr.length ; i++) {
newArr[i] = arr[i];
}
newArr[arr.length] = element;
arr = newArr;
Log.i("tag",Arrays.toString(arr));
}
/**
*线性查找
*/
private int lineSearch(int index) {
for (int i = 0; i <arr.length ; i++) {
if (arr[i] == index){
return i;
}
}
return -1;
}
/**
*删除元素
*/
private void deleteElement(int index) {
if (index<0 || index>arr.length-1){
return;
}
int[] newArr = new int[arr.length-1];
for (int i = 0; i <newArr.length ; i++) {
if (i<index){
newArr[i] = arr[i];
}else{
newArr[i] = arr[i+1];
}
}
arr = newArr;
Log.i("tag",Arrays.toString(arr));
}
/**
* 往数组的末尾添加元素
*/
private void addElementToLast(int element) {
int[] newArr = new int[arr.length+1];
for (int i = 0; i <arr.length ; i++) {
newArr[i] = arr[i];
}
newArr[arr.length] = element;
arr = newArr;
Log.i("tag", Arrays.toString(arr));
}
/**
* 往数组的中间插入元素
* @param index
* @param element
*/
private void insertElement(int index, int element) {
int[] newArr = new int[arr.length+1];
if (index<0 || index>arr.length-1){
return;
}
for (int i = 0; i <arr.length; i++) {
if (i<index){
newArr[i] = arr[i];
}else{
newArr[i+1] = arr[i];
}
}
newArr[index] = element;
arr = newArr;
Log.i("tag",Arrays.toString(arr));
}
}