Dynamic Arrays?

+1 Austin Elliott · February 28, 2015
In my Java class we are currently learning about Dynamic Arrays, but the problem is that the lesson does not teach you how to use them, or the syntax, or anything like that. It tells you what you MIGHT use it for, then it throws you into an example program that is (in my opinion) way too complex for where the class is at right now.

So, what exactly is a Dynamic Array and how do I use it?

Everytime I Google it, it returns something about ArrayList, which is not was I need.

Post a Reply

Replies

- page 1
Oldest  Newest  Rating
+1 Branislav Lazic · February 28, 2015
ArrayList is actually what you need! ArrayList is one of implementation of generic List interface.
List is a collection that takes multiple objects (instances) of same type (it can contain duplicates). It cannot contain objects of different type. I.e. 


List list = new ArrayList();



To add new object call "add" method:


list.add(new Student());



To iterate over list you may use "for each" loop:





for(Student s : list) {
    // do stuff with "s"
}


or "forEach" method (only Java 8):


list.forEach(s -> { // do stuff with "s" });



You may convert it to "stream" to filter results i.e.:


List filteredList = list.stream().filter(s -> s.getAge() > 18).collect(Collections.toList());

Returns list where students age is greater then 18.

etc.
+1 Branislav Lazic · March 1, 2015
That's not dynamic array! 
So basically, you are declaring "data" array of integers that has size of 1 (can have hold 1 integer primitive value).
Then in method "put", you are creating new array called "newData" with new size and you are assigning that array to "data" array.
It's just one array that mutates.

I never in my life used that practice! Yes yes.. List can hold only objects while array can hold primitives too. But that unnecessary amount of verbosity is simply not worth of it.
It's like... using regular "for" loop to iterate over collection of 10 million objects instead of "for each" loop to process your data 0.03 seconds faster!
Plus, in Java 10, collections that take primitives as type argument should arrive. So you will be able to declare your List as:


List<int> list = new ArrayList();
+1 Mathias Frits Rørvik · March 1, 2015
@Branislav Lazic, how is that not a dynamic array?

The example code is an array that grows when more space is needed and It only allows random access (you have to specify which element you want to access 0,1,2 etc.). I would say that the given example definitely satisfies the requirements of a dynamic array.


I agree that ArrayList is a dynamic array, BUT it is also acts as a list. It's more of a hybrid thingy. Which makes it pretty useful, but I think that for Austin Elliott's class they want the students to implement their own data structures.
+1 Branislav Lazic · March 1, 2015
Ok ok. He can use that solution. But it's quite practical. :P
0 Austin Elliott · March 1, 2015
See, when I first Googled it, that's what everybody was saying. But ArrayList is not mentioned at all in the lesson, and it's not used in the example code either.
0 Branislav Lazic · March 1, 2015
Ok, then give us that "example code".
0 Austin Elliott · March 1, 2015
My apologies:


public class DynamicArrayOfInt
{
private int[] data;

public DynamicArrayOfInt()
{
data = new int[1];
}

public int get(int position)
{
if(position >= data.length)
{
return 0;
}else{
return data[position];
}
}

public void put(int position, int value)
{
if(position >= data.length)
{
int newSize = 2 * data.length;
if(position >= newSize)
{
newSize = 2 * position;
}
int[] newData = new int[newSize];
System.arraycopy(data, 0, newData, 0, data.length);
data = newData;

System.out.println("size of dynamic array increased to " + newSize);
}
data[position] = value;
}
}


In a different class:


import java.util.Scanner;

public class ReverseWithDynamicArray
{
public static void main(String[] args)
{
DynamicArrayOfInt numbers;
int numCt;
int num;
Scanner scan = new Scanner(System.in);
numbers = new DynamicArrayOfInt();
numCt = 0;
System.out.println("Enter some positive integers; Enter 0 to end.");

while(true)
{
num = scan.nextInt();
if(num = 0; i++)
{
System.out.println(numbers.get(i));
}
}
}
0 Austin Elliott · March 1, 2015
Oh, my teacher as well as the lesson called it a Dynamic Array.

But anyway, thank you so much! :]
0 Austin Elliott · March 1, 2015
I have another question. In the assignment, we're supposed to print out the first 20 numbers in the Fibonacci Sequence. I slowed down and was able to understand how to use dynamic arrays, but I'm having trouble tying it into the Fibonacci Sequence. This is what I have, but it's not working:


public class class1 {
public static void main(String args[]){

int[] numbers;
numbers = new int[2];
numbers[0] = 0;
numbers[1] = 1;

System.out.println(numbers[0]);

for(int i = 2; i < 20; i++){
numbers = new int[i];
numbers[i] = numbers[i-2] + numbers[i-1];
System.out.println(numbers[i]);
}

}

}


It's giving me an ArrayIndexOutOfBounds exception. 
0 Austin Elliott · March 1, 2015
I don't know what happened but it didn't copy correctly. Anyway:


public class class1 {
public static void main(String args[]){

int[] numbers;
numbers = new int[2];
numbers[0] = 0;
numbers[1] = 1;

System.out.println(numbers[0]);

for(int i = 2; i < 20; i++){
numbers = new int[i];
numbers[i] = numbers[i-2] + numbers[i-1];
System.out.println(numbers[i]);
}

}

}
  • 1
  • 2

Java / Android Development

107,133 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator