Dynamic Arrays 🌱

preview_player
Показать описание
Dynamic arrays arraylists tutorial example explained
Part 1 (00:00:00) theory
Part 2 (00:05:00) practice

#dynamic #arrays #arraylists
Рекомендации по теме
Комментарии
Автор

I forgot to add a get() method for random access 🤦‍♂️
I've included that in the code posted here...

public class Main{

public static void main(String[] args) {

DynamicArray dynamicArray = new DynamicArray(5);

dynamicArray.add("A");
dynamicArray.add("B");
dynamicArray.add("C");


//dynamicArray.insert(0, "X");




System.out.println("size: " + dynamicArray.size);
" + dynamicArray.capacity);
System.out.println("empty: " + dynamicArray.isEmpty());
}
}
public class DynamicArray {

int size;
int capacity = 10;
Object[] array;

public DynamicArray() {
this.array = new Object[capacity];
}
public DynamicArray(int capacity) {
this.capacity = capacity;
this.array = new Object[capacity];
}

public Object get(int index) {
return array[index];
}

public void add(Object data) {

if(size >= capacity) {
grow();
}
array[size] = data;
size++;
}

public void insert(int index, Object data) {

if(size >= capacity) {
grow();
}
for(int i = size; i > index; i--) {
array[i] = array[i - 1];
}
array[index] = data;
size++;
}

public void delete(Object data) {

for(int i = 0; i < size; i++) {
if(array[i] == data) {
for(int j = 0; j < (size - i - 1); j++){
+ j] = array[i + j + 1];
}
array[size - 1] = null;
size--;
if(size <=(int) (capacity/3)) {

}
break;
}
}
}

public int search(Object data) {

for(int i = 0; i < size; i++) {
if(array[i] == data) {
return i;
}
}
return -1;
}

private void grow() {

int newCapacity = (int)(capacity * 2);
Object[] newArray = new Object[newCapacity];

for(int i = 0; i < size; i++) {
newArray[i] = array[i];
}
capacity = newCapacity;
array = newArray;
}

private void shrink() {

int newCapacity = (int)(capacity / 2);
Object[] newArray = new Object[newCapacity];

for(int i = 0; i < size; i++) {
newArray[i] = array[i];
}
capacity = newCapacity;
array = newArray;
}

public boolean isEmpty() {
return size == 0;
}

public String toString() {

String string = "";

for(int i = 0; i < capacity; i++) {
string += array[i] + ", ";
}
if(string != "") {
string = "[" + string.substring(0, string.length() - 2) + "]";
}
else {
string = "[]";
}
return string;
}
}

BroCodez
Автор

I hope you will continue this series. It's just amazing!

s.w.
Автор

Best videos on Data Structure & Algorithm. Thank you for making them!

BN-crel
Автор

This is so sick. Thank you for taking the time to explain and build a working ArrayList. I love people like you that explain things with both graphics and code. I have 0 questions about a topic when you put it together this organized. And the code in the comments (and the commented explanations)... 😙👌

MarshGames
Автор

great LECTURE.
still need to see this over again. love your lectures.

shaistakamran
Автор

What's the difference between making instantiating array as Object[] array and making the class able to hold generic objects using Java generics (i.e. public class DynamicArray<T>)? If we use Object[], wouldn't we have to cast elements to its actual type when retrieving them (e.g. int first = (int) array[0])?

SomduttaSomSinha
Автор

Can you make a video on Java Streams. I would really appreciate it ❤️

malekmousa
Автор

in the .toString() (assuming that you didn't use StringBuilder), wouldn't it be much more efficient and faster by using this?:

String string = "";

for(int i=0;i<size;i++){
if(i>0){
string += ", ";
}
string += array[i];
}

return "[" + string + "]";

instead of doing this?:

for(int i=0;i<size;i++){
string += array[i] + ", ";
}
if(!string.isEmpty()){
string = "["+ string.substring(0, string.length() - 2) + "]";
}
else{
string = "[]";
}


return string;

torekalanae_
Автор

Thank you so much for providing this enlighting tutorial.

phyusinlin
Автор

Currently having java as main programming language in my college, and your videos are really helpful. Thanks!

neil_armweak
Автор

hey brocode. Just a tip for your next videos. If you are going to import images in to your project is it possible to make a link in the descriptions to download the same objects? It's pretty annoying or even sometimes impossible to find an image with the same size etc...

just a tip
love ur videos:)
also make new java videos; if you got time;)

Daaninator
Автор

Yayy, Was waiting for it, your explanations are really great, thanks :)

AbhijeetKumar-cmjh
Автор

Hey bro this tutorial was long and BOOM THANKS BEEN HERE SINCE THE ROBOT SOUNDTRACK🤣

sihleeundefined
Автор

very glad to see your views increasing :) you deserve it and I'm happy that people finally found this gem.

conradpierce
Автор

I feel like in some instances with coding you just have to accept the information and be like well idk why but it works so it works for me? lol

dreful
Автор

In add method, we have to put if condition after size++ for condition of size==capacity

keenlearner
Автор

Thanks bro! Very good and easy to understand explanations

Rumen
Автор

Such as amazing video. This helped me out heaps! thanks. Subscribed !

sadasasdas
Автор

Thanks for this video. It was interesting to see you implement a custom dynamic list rather than just using Java's ArrayList.

oswallt
Автор

I was coming at this from a C angle... I just noticed you weren't in VS code. Eclipse for Java. Got it.

MrDwaynecasey