1 2 Previous Next 15 Replies Latest reply: Feb 2, 2007 1:15 PM by 807599 RSS

    array length

    807599
      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
        • 1. Re: array length
          807599
          Sounds like you need a List instead.

          http://java.sun.com/docs/books/tutorial/collections/index.html
          • 2. Re: array length
            791266
            Aren't these problems usually solved by using a stack?
            • 3. Re: array length
              807599
              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 ?
              • 4. Re: array length
                807599
                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));
                          }
                • 5. Re: array length
                  800323
                  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.
                  • 6. Re: array length
                    807599
                    thank you it works
                    • 7. Re: array length
                      807599
                      do i have to do an iterator tu be able to print what is in it ?
                      or there might be another way
                      • 8. Re: array length
                        doremifasollatido
                        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]
                        • 9. Re: array length
                          807599
                          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
                          • 10. Re: array length
                            807599
                            while(list.iterator().hasNext())
                            {
                             ... // your code
                            }
                            ~Tim
                            • 11. Re: array length
                              doremifasollatido
                              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.
                              • 12. Re: array length
                                807599
                                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...
                                • 13. Re: array length
                                  807599
                                  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
                                  • 14. Re: array length
                                    807599
                                    hehe i'm not to much of a good programmer so bare with me
                                    Should I just get naked and sprawl on a desk? (Image is safe for work!)
                                    1 2 Previous Next