MyArrayList

public class MyArrayList<E> 
{
  private int size; // Number of elements in the list
  private E[] data;
  private int MAXELEMENTS = 100;
  /** Create an empty list */
  public MyArrayList() {
	   data = (E[])new Object[MAXELEMENTS];// cannot create array of generics
       size = 0; // Number of elements in the list
  }
  
  public void add(int index, E e) {   
    // Ensure the index is in the right range
    if (index < 0 || index > size)
      throw new IndexOutOfBoundsException
        ("Index: " + index + ", Size: " + size); 
    // Move the elements to the right after the specified index
    for (int i = size - 1; i >= index; i--)
      data[i + 1] = data[i];
    // Insert new element to data[index]
    data[index] = e;
    // Increase size by 1
    size++;
  }

  public boolean contains(Object e) {
    for (int i = 0; i < size; i++)
      if (e.equals(data[i])) return true;
    return false;
  }

  public E get(int index) {
    if (index < 0 || index >= size)
      throw new IndexOutOfBoundsException
        ("Index: " + index + ", Size: " + size);
    return data[index];
  }
  
  public E remove(int index) {
	if (index < 0 || index >= size)
      throw new IndexOutOfBoundsException
        ("Index: " + index + ", Size: " + size);
    E e = data[index];
    // Shift data to the left
    for (int j = index; j < size - 1; j++)
      data[j] = data[j + 1];
    data[size - 1] = null; // This element is now null
    // Decrement size
    size--;
    return e;
  }
  
  public void clear()
  {
     size = 0;
  }
 
  public MyArrayList<E> merge(MyArrayList<E> param)
  {
	  int i=0; //counter in calling array
	  int j=0; // counter in param array
	  int k=0; // counter in return array
	  MyArrayList<E> returnArray = new MyArrayList();
	  
	  if (this.getSize() ==0) // same as if (size==0)
		  return param;
	  if (param.getSize()==0)
		  return this;
	  if ((this.getSize()+ param.getSize()) > MAXELEMENTS)
		   throw new IndexOutOfBoundsException
        ("Combined list out of bounds");
		
	  // traverse both list until one list is completely done
	  while (i<this.getSize() && j<param.getSize())
	  {
		  // Compare single value from each list and copy smallest into result
		  if (((Comparable)data[i]).compareTo(param.data[j]) <0)
		  {
			returnArray.data[k]= this.data[i];
			k++;
			i++;	
		  }
		  else
		  {
			returnArray.data[k]=param.data[j];
			k++;
			j++;
		  }
	  }
	  
	  // copy remainder of the array
	  if (i < this.getSize())
	  {
		  for (i=i;i<getSize();i++) //for starts at current position
		  {
			returnArray.data[k]= this.data[i];
			k++;
		  }
	  }
	  if (j < param.getSize())
	  {
		  for (j=j;j<param.getSize();j++)
		  {
			returnArray.data[k]=param.data[j];
			k++;
		  }
	  }
	  returnArray.size = k; // set size of return array
	  return returnArray;
  }		  
		  
	  
  public String toString() {
    String result="[";
    for (int i = 0; i < size; i++) {
      result+= data[i];
      if (i < size - 1) result+=", ";
    }
    return result.toString() + "]";
  }

  
  public int getSize() {
    return size;
  }
  
 public boolean sortList() {
    E hold;
	for (int i = 0; i < size-1; i++)
	 {
	   for (int j = 0; j<size-1; j++)
	    {  	 
	     if(((Comparable)data[j]).compareTo(data[j+1])>0)
	      {
	       hold= data[j+1];
	       data[j+1]=data[j];
	       data[j]=hold;
	      }       
	   }
     } 
	 return true;	  	
  }


 
}

Java相关代码片段

how to install java 8 on debian 12

regex caractères spéciaux java

java 11 yum install

manage session in redis spring boot

java: error: release version 19 not supported

bean scope in spring

xml configuration in spring

spring cdi annotations

postconstruct and predestroy in spring

java decode_message

spring lazy initialization

java decorator design pattern

dependency injection in spring

check back pressed in fragment andorid

send email to any domain using java

record java

vs code setup for input/output in java

substring java

receive second word in string java

loose coupling in java

default value of char in java

spring boot repository pattern

spring boot h2 database

add security to spring admin

java islessthan method

Java implementation of recursive Binary Search

java round double

java downcasting

range of fibonacci series in java using recursion

java by anyone

solid java

equals() and hashcode() methods in java

android java map to bundle

android start activity with data

medium java 17

java import util list

bean life cycle in spring

spring boot aop

spring aspect

spring logging

simple java jsp project example

spring boot file watcher implementation example

kotlin loop list

autowired spring

java generics example

spring bean scope

appconfig spring

queue java array

create nan variable java

get string enum java

groovy string to array

find the second largest number in an arrya in java

difference between print() and printf in java

how to check which button is clicked in java

spring boot exception

First Unique characters in a string Java

Substring check as hello in Java

Cherry pickup slon in java

spring boot controller

element count in array java

Count the number of element in java using hashmap

Too long word count in string java

Characters count without using len() in java

Count character of strings in java

print Character at i postion

cutom filter spring security example

spring boot postgresql

repository in spring boot

spring boot service

spring boot model

commandlinerunner spring boot

entity in java spring boot

hellonworld

Make search in postingslist faster

java create dataoutputstream

java create datainputstream

Even Odd sum in java

gRPC Structure Java

PolyLine.java

spring cors