手寫簡單的陣列

2021-10-03 12:10:22 字數 1810 閱讀 4024

實現查詢,新增,刪除操作

array類

package com.company;

public class

array

//無引數的建構函式,預設陣列的容量capacity=

10 public array(

)//獲取陣列中元素的個數

public int getsize(

)//獲取陣列的容量

public int getcapacity(

)//判斷陣列是否為空

public boolean isempty(

)//向所有元素後新增乙個新元素

// public void addlast(

int e)

// data[size]

= e;

// size ++;

//}public void addlast(

int e)

//向第乙個元素前新增乙個新的元素

public void addfirst(

int e)

//在第index個位置插入乙個新元素e

public void add(

int index,

int e)

if(index<0|

|index>size)

for(

int i =size-

1;i>=index;i--)

data[index]

=e; size++;

}//獲取index索引位置的元素

int get(

int index)

return data[index];}

//修改index索引位置的元素為e

void set

(int index,

int e)

//查詢陣列中是否有元素e

public boolean contains(

int e)

return false;

}// 查詢陣列中元素e所在的索引,如果不存在元素e,則返回-

1 public int find(

int e)

}return-1

;}//從陣列中刪除index位置的元素,返回刪除元素

public int remove(

int index)

int ret = data[index]

;for

(int i = index+

1;isize--;

return ret;

}//從陣列中刪除第乙個元素,返回刪除的元素

public int removefirst(

)//從陣列中刪除最後乙個元素,返回刪除的元素

public int removelast(

)//從陣列中刪除元素e

public void removeelement(

int e)

} @override

public string tostring()}

"]")

;return res.tostring();

}}

測試方法main

package com.company;

public class

main

}

測試結果

手寫簡單的KNN演算法

import numpy as np import math class customkneighborsclassifier knn 自定義實現類 def init self,k 3 初始化賦值,x train 特徵測試集變數 y train 標量測試集變數 res class 結果 param ...

手寫簡單版本的RPC

一 實現思路 基於socket與serversocket網路程式設計 二 實現方案 1 rpc server 1 rpc server api a.建立 isayhellow public inte ce isayhellow b.建立 istudyhard public inte ce istud...

手寫簡單LinkList集合

package com.zl.ext.collection packagb com.zl.ext.collection author fab description 基於雙向鍊錶實現 date modified by public class mylinklist 建構函式 mylinklist 新...