java算法分析与设计_Java算法分析与设计!?

package com.chen.arithmetic_test.list_test;

/**

* Created by ChenMP on 2017/7/3.

*/

public interface List {

//获得长度

public int size();

//插入元素

public boolean insert(int index, Object o) throws Exception;

//新增元素

public boolean add(Object o) throws Exception;

//删除元素

public boolean remove(int index) throws Exception;

//获取元素

public Object get(int index) throws Exception;

//判断线性表是否为空

public boolean isEmpty();

package com.chen.arithmetic_test.list_test;

/**

* Created by ChenMP on 2017/7/3.

*/

public class SequenceList implements List {

private Object[] listArray;//对象数组

private int size;//对象数组长度

private boolean isFixed;//是否限定对象数组长度

public SequenceList() {

this.size = 0;

this.listArray = new Object[3];//默认对象数组固定长度为3

this.isFixed = false;//不限定对象数组长度

}

public SequenceList(int length) {

this.listArray = new Object[length];

this.size = 0;

this.isFixed = true;//限定对象数组长度

}

@Override

public int size() {

return size;

}

@Override

public boolean insert(int index, Object o) throws Exception {

if(index > size || index < 0)

throw new Exception("IndexOutOfBoundsException");

if(index==size && size==this.listArray.length && this.isFixed)

throw new Exception("IndexOutOfBoundsException");

if(index==size && size==this.listArray.length && !this.isFixed) {

Object[] newListArray = new Object[this.listArray.length + 10];

System.arraycopy(listArray, 0, newListArray, 0, listArray.length);

newListArray[index] = o;

this.listArray = newListArray;

this.size++;

return true;

}

if (index == size && size

listArray[index] = o;

this.size++;

return true;

}

if(index < size && index >= 0) {

listArray[index] = o;

return true;

}

return false;

}

@Override

public boolean add(Object o) throws Exception {

if(this.size==this.listArray.length && this.isFixed)

throw new Exception("IndexOutOfBoundsException");

if(this.size==this.listArray.length && !this.isFixed) {

Object[] newListArray = new Object[this.listArray.length + 10];

System.arraycopy(listArray, 0, newListArray, 0, listArray.length);

newListArray[size] = o;

this.listArray = newListArray;

this.size++;

return true;

}

if(this.size

listArray[this.size] = o;

this.size++;

return true;

}

return false;

}

@Override

public boolean remove(int index) throws Exception {

if(index < 0 || index >= size)

throw new Exception("IndexOutOfBoundsException");

System.arraycopy(listArray, 0, listArray, index, listArray.length-index);

this.size--;

return true;

}

@Override

public Object get(int index) throws Exception {

if(index < 0 || index >= size)

throw new Exception("IndexOutOfBoundsException");

return this.listArray[index];

}

@Override

public boolean isEmpty() {

return this.size>0?false:true;

}

@Override

public String toString() {

StringBuilder sb = new StringBuilder();

for (Object o : this.listArray) {

if(null != o)

sb.append(o).append(" ,");

}

return sb.toString();

}

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

测试类

package com.chen.arithmetic_test.list_test;

/**

* Created by ChenMP on 2017/7/3.

*/

public class TestList {

public static void main(String[] args) throws Exception {

List list = new SequenceList(3);

list.insert(0,0);

list.add(1);

list.add(2);

// list.add(3);

System.out.print("测试定长list: " + list.toString() + "|| list长度为: " + list.size());

System.out.println();

List list2 = new SequenceList();

list2.add(0);

list2.add(1);

list2.add(2);

list2.add(3);

System.out.print("测试不定长list: " + list2.toString() + "|| list长度为: " + list2.size());

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
利用Java编写的几种经典问题算法: 1.设a[0:n-1]是一个有n个元素的数组,k(0<=k<=n-1)是一个非负整数。 试设计一个算法将子数组a[0:k]与a[k+1,n-1]换位。要求算法在最坏情况下耗时O(n),且只用到O(1)的辅助空间。 2.在一个圆形操场的四周摆放着n堆石子。现要将石子有次序地合并成一堆。规定每次只能选相邻的2堆石子合并成新的一堆,并将新的一堆石子数记为该次合并的得分。试设计一个算法,计算出将n堆石子合并成一堆的最小得分和最大得分,并分析算法的计算复杂性。 3.设磁盘上有n个文件f1,f2,…,fn,每个文件占用磁盘上的1个磁道。这n个文件的检索概率分别是p1,p2,…,pn,且 =1。磁头从当前磁道移到被检信息磁道所需的时间可用这2个磁道之间的径向距离来度量。如果文件fi存放在第i道上,1≦i≦n,则检索这n个文件的期望时间是对于所有的i<j,time+=pi*pj*d(i,j) 。其中d(i,j)是第i道与第j道之间的径向距离|i-j|。磁盘文件的最优存储问题要求确定这n个文件在磁盘上的存储位置,使期望检索时间达到最小。试设计一个解决此问题的算法,并分析算法的正确性与计算复杂度。 4.最小重量机器设计问题。设某一机器由n个部件组成,每一种部件可以从m个不同的供应商处购得。设wij是从供应商j处购得的部件i的重量,cij是相应的价格。试设计一个算法,给出总价格不超过c的最小重量机器设计
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值