Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class OwnArrayList {
- Object [] tab;
- int currentSize;
- int numberOfElements=0;
- public OwnArrayList(int startSize){
- tab = new Object[startSize];
- currentSize=startSize;
- }
- public OwnArrayList(){
- tab = new Object[10];
- currentSize=10;
- }
- public Object get(int index){
- if(index<currentSize && index>-1){
- return tab[index];
- }
- else {System.out.println("Incorrect index");
- return null;
- }
- }
- public Object set(int index, Object o) {
- if (index < currentSize && index > -1) {
- Object actual = tab[index];
- tab[index] = o;
- return actual;
- }
- else {System.out.println("Incorrect index");
- return null;
- }
- }
- private Object[] increaseCapacity() {
- Object [] newTab;
- double temp=0;
- if(currentSize<=5){
- temp =10;
- }
- else if(currentSize>5){
- temp = 1.6*currentSize;}
- newTab = new Object[(int)temp];
- currentSize=newTab.length;
- for (int i = 0; i <tab.length; i++) {
- newTab[i] = tab[i];
- }
- tab=newTab;
- return tab;
- }
- public int size(){return numberOfElements;}
- public boolean add(Object o) {
- int currentLength = currentSize;
- if (isFull()) {
- increaseCapacity();
- tab[currentLength] = o;
- numberOfElements++;
- return true;
- }
- else if (!isFull() && tab[0] != null) {
- for (int i = currentSize - 1; i >= 0; i--) {
- if (tab[i] != null) {
- tab[i + 1] = o;
- numberOfElements++;
- return true;
- }
- }
- }
- else if (!isFull() && tab[0] == null) {
- tab[0] = o;
- numberOfElements++;
- return true;
- }
- return true;
- }
- public int find(Object o){
- for(int i=0;i<currentSize;i++){
- if(tab[i].equals(o)){
- return i;}
- }
- return -1;
- }
- private boolean isFull(){
- return numberOfElements==currentSize;
- }
- public boolean remove(Object o) {
- if (find(o) == -1) {
- return false;
- } else {
- Object[] newTab = new Object[currentSize - 1];
- int i = 0;
- for (int j = 0; j < currentSize; j++) {
- if (j < i) {
- newTab[j] = tab[j];
- } else if (j > i) {
- newTab[j - 1] = tab[j];
- }
- }
- tab = newTab;
- currentSize--;
- numberOfElements--;
- return true;
- }
- }
- public void printer(){
- for(int i=0;i<currentSize;i++)
- System.out.println(tab[i]);
- }
- public static void main(String []args){
- OwnArrayList list = new OwnArrayList(3);
- System.out.println(list.size());
- list.add("one");
- list.add("two");
- list.add("three");
- list.add("four");
- list.add("five");
- list.add("six");
- list.add("seven");
- list.add("eight");
- list.add("nine");
- list.add("ten");
- list.add("eleven");
- System.out.println(list.isFull());
- System.out.println(list.numberOfElements);
- list.printer();
- list.remove("one");
- list.set(3,"$$$");
- list.printer();
- list.find("two");
- System.out.println(list.size());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement