array length

I know it is possible tu increase the array size but i don't know how to do so. The reason why i want to this is because i need to add new items dynamicly to the array. Depending on the lenght of what is typed in.

exemple. If i type the first time 24+13 i want the array to be of length 3 but if i type 12+3-4*5 i want the array to be able to increase to a length of 7 so i can afterwards sum everything.

thank you for the help

[447 byte] By [wclefa] at [2007-11-26 17:07:00]
# 1
Sounds like you need a List instead. http://java.sun.com/docs/books/tutorial/collections/index.html
zadoka at 2007-7-8 23:34:49 > top of Java-index,Java Essentials,New To Java...
# 2
Aren't these problems usually solved by using a stack?
kajbja at 2007-7-8 23:34:49 > top of Java-index,Java Essentials,New To Java...
# 3
ok but with the arrayList if i use the add fonction can i still add a new element to a list of size n and by using add it will automaticly do arrayList is now n+1 ?
wclefa at 2007-7-8 23:34:49 > top of Java-index,Java Essentials,New To Java...
# 4

> ok but with the arrayList if i use the add fonction

> can i still add a new element to a list of size n and

> by using add it will automaticly do arrayList is now

> n+1 ?

Try it:

List<String> list = new ArrayList<String>();

for(int i =0; i<10; i++){

list.add(String.valueOf(i));

}

zadoka at 2007-7-8 23:34:49 > top of Java-index,Java Essentials,New To Java...
# 5
Arrays are fixed in size; you cannot increase the size once created.ArrayLists will increase their size automatically.Stacks will increse their size automatically.Collections, in general, will grow dynamically.
jbisha at 2007-7-8 23:34:49 > top of Java-index,Java Essentials,New To Java...
# 6
thank you it works
wclefa at 2007-7-8 23:34:49 > top of Java-index,Java Essentials,New To Java...
# 7
do i have to do an iterator tu be able to print what is in it ?or there might be another way
wclefa at 2007-7-8 23:34:49 > top of Java-index,Java Essentials,New To Java...
# 8

> do i have to do an iterator tu be able to print what

> is in it ?

> or there might be another way

If you don't care what the output format is:

System.out.println(list);

For a list containing three Strings "24", "+", "13", that would print:

[24, +, 13]

doremifasollatidoa at 2007-7-8 23:34:49 > top of Java-index,Java Essentials,New To Java...
# 9

thank you for the information but this is what i would like to do

if(list.listIterator().hasNext()) {

System.out.println ("element is " + list.iterator().next());

}

but when i do so, if i typed in 1+2 the output will be

Element is 1 and i won't have another output for the Element is + and Element is 2

wclefa at 2007-7-8 23:34:49 > top of Java-index,Java Essentials,New To Java...
# 10
while(list.iterator().hasNext()){ ... // your code}~Tim
SomeoneElsea at 2007-7-8 23:34:49 > top of Java-index,Java Essentials,New To Java...
# 11

You had:

if(list.listIterator().hasNext()) {

System.out.println ("element is " + list.iterator().next());

}

You don't want to keep getting new iterators. Unless you need to go backwards, an Iterator will work (no need for ListIterator). You also need a loop:

Iterator iter = list.iterator();

while (iter.hasNext())

{

System.out.println ("element is " + iter.next());

}

To format code nicely, use the button above the posting box.

doremifasollatidoa at 2007-7-8 23:34:49 > top of Java-index,Java Essentials,New To Java...
# 12

If you are using version 5 or later (didn't I see code with generics earlier?), you can use the enhanced for loop. Behind its synatic sugar, it is calling the iterator() method, defining a local iterator and using its hasNext() and next() methods, but what you see is just this:

import java.util.*;

public class Example {

public static void main(String[] args) {

List<String> words = new ArrayList<String>(Arrays.asList("Hello","world","howdy","doo"));

for(String word : words) {

System.out.println(word);

}

}

}

Tasty...

DrLaszloJamfa at 2007-7-8 23:34:49 > top of Java-index,Java Essentials,New To Java...
# 13

thx you guys

hehe i'm not to much of a good programmer so bare with me

This is how it is my Array is build in one class i would like to pass this array in parameter to a new class and have this classe do the math

i have an idea of how to reorganise my array and do the math operation but how do i call a class from another classe and have the array i've build pass in parameter. without have a null array

public void SendResult(ArrayList tab[]){

resultats = new Result();

}

this is what i did but it doesnt work

wclefa at 2007-7-8 23:34:49 > top of Java-index,Java Essentials,New To Java...
# 14
> hehe i'm not to much of a good programmer so bare with me[url= http://www.unitedmedia.com/comics/dilbert/archive/images/dilbert20071832630111.gif]Should I just get naked and sprawl on a desk?[/url] (Image is safe for work!)
DrLaszloJamfa at 2007-7-8 23:34:49 > top of Java-index,Java Essentials,New To Java...
# 15

Well, you create and instance of that class in the class you will pass from and then provide a suitable method. ie one that takes and argument (and probably returns the same) of an array list in the class you will pass too.

It is just like calling a class with an int or String, set the parameter and pass in the string through the instance.

kikemellya at 2007-7-21 16:57:36 > top of Java-index,Java Essentials,New To Java...