logo elektroda
logo elektroda
X
logo elektroda

[java] Setting Size for Dynamic Array in Main Method: User-defined String Name[] Array Size

przemo_wielki 29439 6
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 7232730
    przemo_wielki
    Level 24  
    Hello,

    I have a problem with setting the size for a dynamic array, it's best to give an example:

    
    public class Osoba {
    String imie[]; //dynamiczna tablica 
    }
    
    public class Przyklad{
     public static void main(String[] args){
      int rozmiar=5;
    
      Osoba nowy[] = new Osoba();//dynamiczna tablica obiektow 
      
     }
    }
    

    Question: how to set the size of the String name [] in the main method? I would like the user, while the program is running, to choose how many names (eg) can be assigned to a person (the size of the dynamic array Name []).

    ps: I'm a beginner, so please be understanding;]
  • ADVERTISEMENT
  • ADVERTISEMENT
  • #3 7233593
    mrio
    Level 10  
    [code:1:9457c0dc6f]
    rozmiar = 10;
    Osoba[] osoby = new Osoba[rozmiar];
    for (int i=0; i
  • #4 7233758
    przemo_wielki
    Level 24  
    In the previous code a bug crept in (I wrote hard without a compiler) correct object creation should look like this:

    
                Osoba[] nowy;//dynamiczna tablica obiektow
                nowy = new Osoba[rozmiar];
    
    //lub  Osoba[] nowy = new Osoba[rozmiar];
    


    Only I know it exactly. I want to set the size for the object field new [] from the main () function; is it possible?

    I'm talking about :
    
    String imie[]; //dynamiczna tablica 
    
  • ADVERTISEMENT
  • Helpful post
    #5 7233815
    Dżyszla
    Level 42  
    in a constructor or via a public variable.
  • ADVERTISEMENT
  • Helpful post
    #6 7233948
    mrio
    Level 10  
    
    public class Main {
        public static void main(String[] args) {
            int rozmiar = 5;
            Osoba[] osoby = new Osoba[rozmiar];
            osoby[0] = new Osoba(2);
            osoby[1] = new Osoba(1);
            // ... itd.
        }
    }
    class Osoba {
        String[] imiona;
        // Tworzy obiekt Osoba o podanej liczbie imion.
        Osoba(int liczbaImion){
            imiona = new String[liczbaImion];
        }
    }
  • #7 7234030
    przemo_wielki
    Level 24  
    Heh actually;] and I was combining miracles of wreaths :D

    The constructor did the trick.

Topic summary

The discussion revolves around setting the size of a dynamic array for a user-defined class `Osoba` in Java. The user seeks to allow dynamic input for the size of a `String` array within the `Osoba` class, specifically for storing names. Various responses suggest initializing the `Osoba` array with a specified size and using a constructor to define the size of the `String` array for names. The correct approach involves creating an `Osoba` array in the `main` method and utilizing a constructor to allocate the `String` array based on user-defined size.
Summary generated by the language model.
ADVERTISEMENT