tags:

views:

839

answers:

5

Hi, I please for your help.

I apologize for my English, it is still learning. I do one application in GUI in Java. I have one problem.

In this application I have an ArrayList that I want to meet with data.

User clicks on the button and data from JTextfield shall be placed in Arraylist.

When they do so, it is inserted only into the first row. Other entries in Arraylist are not.

Ask me please how do I do that.

Here is code of my application:

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); 
            }
        }
+3  A: 

Just use ArrayList.add(arraytext), not ArrayList.add(0, arraytext). You don't really care what position it ends up in, 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); 
  }

with

arlist.add(arraytext);
Paul Tomblin
A: 

Ok, let me see if I get this correctly.

You have a JTextField.

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

If is not empty it should go in the following positions?

That would 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.

I hope this helps.

OscarRyz
A: 

if you care about it's position, try adding it to a SortedSet (probably a TreeSet)

Ray Tayek
+4  A: 

All the answers you received till now, are correct. Precisely, if you want to add that into an arraylist and you don't really care about the position, just use

arrayList.add(arrayText)

Otherwise, using a TreeSet instead would help.

The reason I attempted this question is because, your method arguments are producing foul code smell. I have few questions with appropriate suggestions.

  • Do you really need 8 arguments? Why don't define a POJO (Plain Old Java Object)
  • What will happen if you think that you need to pass one more thing as an argument? Would you increase it to 9? POJO would makes sense and is a cleaner way definitely
  • Why are you concatenating all values into one single string separated by a comma, as a delimiter? Why not have a list of POJOs? Would it not serve your purpose? Actually this way I don't know how many times you need to manipulate that string, which will result in unnecessary work everytime you need a single particular element and it affects performance, clearly.
Adeel Ansari
Good point. Something isn't quite right if you need to do all that concatenation.
Ken Wootton
Yes, good point. +1
Paul Tomblin
A: 

Hi,the user Jirka am i.Im registered yesterday under name Gisat. Everything you write here, I have tried, but without result. I do not know where I got an error. Find please someone who would help me? My mail is:[email protected] and icq number: 343410581.

I do not know how send e-mails for people here. If someone wanted to help me, so feel free to send him a set of my project.

Al code of my method is:

print( public void ulozeni_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){
     int stav=1;
    String hlaska_prazdny;




    //Celé jméno,určené do výsledků
    String cele_jmeno=jmeno_zad+" "+prijmeni_zad;

    //Uložení do arraylistu
    if(!jmeno_zad.isEmpty() && !prijmeni_zad.isEmpty() && !rodne_cislo_zad.isEmpty() && !mesto_zad.isEmpty() && !spz_zad.isEmpty() && !barva_zad.isEmpty() && !vin_zad.isEmpty() && !znacka_zad.isEmpty()){
        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, arraytext);}


        System.out.println(arlist.get(0));
    }
    else{
        hlaska_prazdny="Nevyplnili jste některé pole, vyplňte jej prosím!!";
        JOptionPane.showMessageDialog(null,hlaska_prazdny,"Nevyplnili jste pole!!",JOptionPane.OK_OPTION);
    }

    if(!arlist.isEmpty()){
        for (Iterator<String> i = arlist.iterator(); i.hasNext(); ) {
            System.out.println(i.next());
        }
    }

}

private void vloz(String text){
   arlist.add(text);
});

Thank you in advance for any help

Formatted (mainly indentation).
Michael Myers
It looks like you've defined a private method "vloz" but have not used it. Try replacing the section "int posl=arlist.size(); ... System.out.println(arlist.get(0));" with "vloz(arraytext);".
Michael Myers
Also, that "System.out.println(arlist.get(0))" statement only prints the first element of the list. Try changing it to "System.out.println(arlist)".
Michael Myers