ArrayList and the problem with adding elements

I am sorry for your help.

I apologize for my english, he is still learning. I am making one GUI application in Java. I have one problem.

In this application, I have an ArrayList that I want to come across with data.

The user clicks a button and the data from the JTextfield is put into an Arraylist.

When they do, it is inserted only on the first line. Other entries in the Arraylist are not.

Please ask me how to do this.

Here is my application code:

public void save_to_array(String jmeno_zad,
                          String prijmeni_zad,
                          String rodne_cislo_zad,
                          String mesto_zad,
                          String spz_zad,
                          String barva_zad, 
                          String vin_zad, 
                          String znacka_zad){


            String arraytext=prijmeni_zad+ ","+
            jmeno_zad+ ","+ 
            rodne_cislo_zad+ ","+
            mesto_zad+","+
            spz_zad+","+
            barva_zad+","+
            vin_zad+","+
            znacka_zad; 

            int posl=arlist.size(); 
            if(arlist.isEmpty()||posl==1){ 
                arlist.add(0,arraytext);
            } 

            if(!arlist.isEmpty()&& posl>1){ 
                arlist.add(posl-1, cele_jmeno); 
            }
        } 

      

+1


source to share


4 answers


Just use ArrayList.add (arraytext) and not ArrayList.add (0, arraytext). You don't care what position he hits, do you?

Replace

 int posl=arlist.size(); 
 if(arlist.isEmpty()||posl==1){ 
            arlist.add(0,arraytext);
  }
  if(!arlist.isEmpty()&& posl>1){ 
            arlist.add(posl-1, cele_jmeno); 
  }

      



from

arlist.add(arraytext);

      

+4


source


All the answers you have received so far are correct. Precisely if you want to add this to the arraylist and you don't really care about the position, just use

arrayList.add(arrayText)

      

Otherwise, using a TreeSet will help.



The reason I tried to ask this question is because the arguments to your method generate bad code smell. I have few questions with relevant proposals.

  • Do you really need 8 arguments? Why not define a POJO (Plain Old Java Object)
  • What happens if you think you need to pass one more thing as an argument? Could you increase it to 9? POJO makes sense and is a clearer way.
  • Why are you concatenating all values ​​into one comma separated string like separator? Why not make a list of POJOs? Doesn't that serve your purpose? In fact, this way I don't know how many times you need to manipulate this string, which will lead to unnecessary work every time you need one specific element, and this clearly affects performance.
+4


source


Ok, let's see if I do it right.

You have a JTextField.

As far as your code goes, I understand that if the arraylist is empty, you should put the text in the first position.

If not empty, it should go in the following positions:

It will be like this:

public void saveToArray(  String jmeno_zad,
                          String prijmeni_zad,
                          String rodne_cislo_zad,
                          String mesto_zad,
                          String spz_zad,
                          String barva_zad, 
                          String vin_zad, 
                          String znacka_zad) {


            String arrayText=prijmeni_zad+ ","+
                             jmeno_zad+ ","+ 
                             rodne_cislo_zad+ ","+
                             mesto_zad+","+
                             spz_zad+","+
                             barva_zad+","+
                             vin_zad+","+
                             znacka_zad; 

            arrayList.add( arrayText );

 }

      

By default, if the arraylist is empty, the text will be in the 1st position. The next addition will be in the 2nd, 3rd, etc.

Hope this helps.

0


source


if you like it try adding it to SortedSet (maybe TreeSet)

0


source







All Articles