Link to home
Start Free TrialLog in
Avatar of EMB01
EMB01Flag for United States of America

asked on

Creating array of unspecified length in java, or adding to an array beyond dimension

I want to be able to create an array that I can add to without fear of going out of dimension.  For instance, the following code:

	public static void main(String[] args)
	{
		
		String[] array = new String[3];
		array[0] = "string1";
		array[1] = "string2";
		array[2] = "string3";
		
		for (String string : array)
		{
		
			System.out.println(string);
			
		}
		
		// following line makes the array go out of bounds
		array[4] = "string3";
		
	}

Open in new window


I don't really want to have to use a collection (or vector?) but I will if that's the only way to achieve dynamic array functionality.  Thank you.
Avatar of for_yan
for_yan
Flag of United States of America image

You have to use Veactor or ArrayList

You cannot create array of unspecified dimenion in Java
Arrays need to be created with specified length
(tis lenght can bwe  avraible of course but shouldb be known at the moment of creation
Avatar of EMB01

ASKER

Could you demonstrate to me in the current context how to use vector (and or, preferably and) arrayList as I am new to java?  Also, what about the following article where they say you can use "make" to do something like this http://stackoverflow.com/questions/664389/byte-array-of-unknown-length-in-java
So your best bet is to create say ArrayList and thenafter you populated it and you nkknow there will be no additions there are methods which wioll make array of it.
Or just keep it in the ArrayList all the way
This is not array - they suggsest to use stream - it is quote different in that articlae - btettr use ArrayList
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ArrayList especuially oocnvenienet when you populate say from adtabnase table where you do not know initially how many you have
Avatar of EMB01

ASKER

Thanks, and any code you could send to demonstrate these suggestions in this context would be helpful for me to study.
read this preamble and look at the methods:
http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html

ArrayList is probably the most important statndrad Java class after String
I think my code above shows the same as your with array

You can also use

for (String a : ar)
      
construct to iterate through ArrayList the same way as with the array
Avatar of EMB01

ASKER

I am native to PHP and their documentation provides not just acceptable params, but also working examples of the code.  I don't see any working examples on this java doc so it's hard for me to understand.  I assume there are also utilities to be imported and such, but I don't see where that is listed, either.  Thanks for understanding.
do not knwo if you use JDBC but with databses it will also be easy,
usually like that:

ResultSet rs = stmt.eexcuteQuery("select...");
while(rs.next()){

ar.add(rs.getString(1);

}
look here:
http://www.javadeveloper.co.in/java-example/java-arraylist-example.htm

or type
ArrayList Java code examples
in google - you'll find lots of those
Avatar of EMB01

ASKER

thank you very much.  those may be very helpful.  do you have any examples of java doc sites, like this one for php:

http://www.php.net/manual/en/function.str-replace.php

it's very helpful because it has acceptable params, example use, notes from other users, common "gatchas," etc.
Java has its own style of javadoc (as you saw) - it is a bit different and only some classes have examples in the preamble - but you'll soon get accustomed, and when you need some example of code usually google will help

I don't think we can find exactly the same kind of site for java
there are many lke that, but none to be considered standard other than API sites
Avatar of EMB01

ASKER

okay, thanks.  i'll keep trying to learn more