samedi 5 avril 2014

Java - ajout d'éléments dans arraylist - Stack Overflow


Please consider the following code:


while(rs.next()){    
String ipAddress = rs.getString("IP_vch");
System.out.println("The value of ipAddress is:"+ipAddress);
MainConnection[i] = DriverManager.getConnection("jdbc:mysql://" + ipAddress + ":3306/test",RemoteUser,RemotePass);
}// END Of WHILE (rs.next())

I am defining an arraylist of String after the line System.out.println("The value of ipAddress is:"+ipAddress); as follows:


List AddressList = new ArrayList;


AddressList.add(ipAddress);



  1. So, does above step will add all the values that are in the string variable ipAddress?


  2. I want to establish connection in my next step something like as shown in above code as follows:

    MainConnection[i] = DriverManager.getConnection("jdbc:mysql://" + ipAddress + ":3306/test",RemoteUser,RemotePass);



For that I don't see nay put method in the ArrayList API description here.




yes. it will add all the values of arraylist ipAddress. use ipAddress.get(index) to access an ipaddress in a list.




I am not sure what is that you are trying to so. But if you are trying to store all the ip address in the list then you should define


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

Outside your while loop and add ip address to list inside the loop. Defining ArrayList inside the loop is creating a new list for each iteration.




I would really expect something like this:


// Create array list
List<String> AddressList = new ArrayList<String>();

// Connect to database
Connection =
DriverManager.getConnection("jdbc:mysql://" + ipAddress + ":3306/test",RemoteUser,RemotePass);

// Make Query
...

// Fetch results and add them to the list
while(rs.next()){
String ipAddress = rs.getString("IP_vch");
System.out.println("The value of ipAddress is:"+ipAddress);
AddressList.add(ipAddress);
...



List<String> addressList = new ArrayList<String>(); // will do it.. using Generics is always better.


Please consider the following code:


while(rs.next()){    
String ipAddress = rs.getString("IP_vch");
System.out.println("The value of ipAddress is:"+ipAddress);
MainConnection[i] = DriverManager.getConnection("jdbc:mysql://" + ipAddress + ":3306/test",RemoteUser,RemotePass);
}// END Of WHILE (rs.next())

I am defining an arraylist of String after the line System.out.println("The value of ipAddress is:"+ipAddress); as follows:


List AddressList = new ArrayList;


AddressList.add(ipAddress);



  1. So, does above step will add all the values that are in the string variable ipAddress?


  2. I want to establish connection in my next step something like as shown in above code as follows:

    MainConnection[i] = DriverManager.getConnection("jdbc:mysql://" + ipAddress + ":3306/test",RemoteUser,RemotePass);



For that I don't see nay put method in the ArrayList API description here.



yes. it will add all the values of arraylist ipAddress. use ipAddress.get(index) to access an ipaddress in a list.



I am not sure what is that you are trying to so. But if you are trying to store all the ip address in the list then you should define


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

Outside your while loop and add ip address to list inside the loop. Defining ArrayList inside the loop is creating a new list for each iteration.



I would really expect something like this:


// Create array list
List<String> AddressList = new ArrayList<String>();

// Connect to database
Connection =
DriverManager.getConnection("jdbc:mysql://" + ipAddress + ":3306/test",RemoteUser,RemotePass);

// Make Query
...

// Fetch results and add them to the list
while(rs.next()){
String ipAddress = rs.getString("IP_vch");
System.out.println("The value of ipAddress is:"+ipAddress);
AddressList.add(ipAddress);
...


List<String> addressList = new ArrayList<String>(); // will do it.. using Generics is always better.

0 commentaires:

Enregistrer un commentaire