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

Oldest  Newest  Rating
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 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]);
}

}

}
0 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.
  • 1

Java / Android Development

129,681 followers
About

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

Links
Moderators
Bucky Roberts Administrator