-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
116 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import java.util.Arrays; | ||
|
||
public class ArrayComRodinhas { | ||
|
||
int[] arrayInterno; | ||
|
||
private int quantElementos; | ||
|
||
|
||
public ArrayComRodinhas() { | ||
this.arrayInterno = new int[1]; | ||
this.quantElementos = 0; | ||
} | ||
|
||
public void add(int elemento) { | ||
if (this.quantElementos == this.arrayInterno.length) { | ||
redimensionarArrayInterno(); | ||
} | ||
|
||
this.arrayInterno[this.quantElementos++] = elemento; | ||
} | ||
|
||
public int get(int posicao) { | ||
if (posicao < 0 || posicao >= this.quantElementos) { | ||
throw new ArrayIndexOutOfBoundsException(posicao); | ||
} | ||
return this.arrayInterno[posicao]; | ||
} | ||
|
||
private void redimensionarArrayInterno() { | ||
int[] novoArray = new int[this.arrayInterno.length * 2]; // cresce em PG | ||
for (int i = 0; i < quantElementos; i++) { | ||
novoArray[i] = this.arrayInterno[i]; | ||
} | ||
this.arrayInterno = novoArray; | ||
} | ||
|
||
public int getQuantElementos() { | ||
return this.quantElementos; | ||
} | ||
|
||
public String toString() { | ||
return Arrays.toString(this.arrayInterno); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import java.util.Random; | ||
|
||
public class Principal { | ||
|
||
public static void main(String[] args) { | ||
|
||
Random random = new Random(); | ||
|
||
ArrayComRodinhas meuArray = new ArrayComRodinhas(); | ||
|
||
int N = 100_000; | ||
|
||
long inicio = System.currentTimeMillis(); | ||
for (int i = 0; i < N; i++) { | ||
meuArray.add(random.nextInt(100)); | ||
} | ||
long duracao = System.currentTimeMillis() - inicio; | ||
|
||
System.out.printf("duracao = %.3f\n", duracao / 1000f); | ||
|
||
System.out.printf("O array agora tem %d elementos (o tamanho físico é %d).", | ||
meuArray.getQuantElementos(), meuArray.arrayInterno.length); | ||
|
||
} | ||
} |