Dynamic Arrays?

 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.

Replies

- page 1

 Austin Elliott · March 1, 2015 It did it again, it insert urls into my code that wasn't there...Sorry about that, nothing I can do. 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]); } } }`` 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. Branislav Lazic · March 1, 2015 Ok ok. He can use that solution. But it's quite practical. 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. Austin Elliott · March 1, 2015 Oh, my teacher as well as the lesson called it a Dynamic Array.But anyway, thank you so much! :] 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 list = new ArrayList();`` 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)); } }}`` Branislav Lazic · March 1, 2015 Ok, then give us that "example code". 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.
• 1
• 2

Java / Android Development

107,103 followers