Search Results

Search found 1459 results on 59 pages for 'arraylist'.

Page 1/59 | 1 2 3 4 5 6 7 8 9 10 11 12  | Next Page >

  • Convert Java Arraylist return from Java WebService to C# Arraylist

    - by TTCG
    In my C# program, I would like to consume the Java Web Service Method which replies the java.Util.ArrayList. I don't know how to convert the Java ArrayList to C# ArrayList. This is Java Codes @WebMethod @WebResult(name = "GetLastMessages") @SuppressWarnings("unchecked") public ArrayList<Message> getLastMessages(@WebParam(name = "MessageCount")int count) { .... .... return messages; } This is C# Codes MessagesService.MessagesService service = new MessagesService.MessagesService(); System.Collections.ArrayList arr = (System.Collections.ArrayList)service.getLastMessages(10); I got the following Error in C# Cannot convert type 'WindowsFormsApplication1.MessagesService.arrayList' to 'System.Collections.ArrayList' How can I cast these Java ArrayList to C# ArrayList

    Read the article

  • convert ArrayList.toString() back to ArrayList in one call

    - by dotnetnewbie
    I have a toString() representation of an ArrayList. Copying the toString() value to clipboard, I want to copy it back into my IDE editor, and create the ArrayList instance in one line. In fact, what I'm really doing is this: my ArrayList.toString() has data I need to setup a unit test. I want to copy this ArrayList.toString() into my editor to build a test against this edge case I don't want to parse anything by hand My input looks like this: [15.82, 15.870000000000001, 15.92, 16.32, 16.32, 16.32, 16.32, 17.05, 17.05, 17.05, 17.05, 18.29, 18.29, 19.16] The following do not work: Arrays.asList() google collections Lists.newArrayList() Suggestions?

    Read the article

  • JAVA: Sort ArrayList<ArrayList<Integer>> on multiple columns

    - by Bob
    First, I did do my homework searching before posting here. My requirement seems to be slightly different compared to questions posted out there. I have a matrix like ArrayList<ArrayList<Integer>> in the following form | id1 | id2 | score | |-----|-----|-------| | 1 | 3 | 95% | | 1 | 2 | 100% | | 1 | 4 | 85% | | 1 | 5 | 95% | | 2 | 10 | 80% | | 2 | 15 | 99% | I want to sort the matrix column-wise (first using score, then the id1). I already have the id1 in a sorted manner. That means I also need to sort all records with the same id1 first by using score, second by the id2. The reason for doing this is to create a ranking of the id2 in each id1. The result for the above example would be: | q_id | d_id | rank | score | |------|------|------|-------| | 1 | 2 | 1 | 100% | | 1 | 3 | 2 | 95% | | 1 | 5 | 3 | 95% | | 1 | 4 | 4 | 85% | | 2 | 15 | 1 | 99% | | 2 | 10 | 2 | 80% | How can I achieve this in Java using some built-in methods of collections?

    Read the article

  • Data from 6 ArrayLists into a single JTable - Java Swing

    - by Splunk
    I have created a JTable which is populated by various arraylists which get their data from a text list using a "~" to split. The issue I am having is that the table is displaying all data from the list on a single row. For example: Column1 Column2 Column2 Column2 Column3 Column4 1,2,3,4,5 1,2,3,4,5 1,2,3,4,5 1,2,3,4,5 1,2,3,4,5 1,2,3,4,5 When I want it to display Column1 Column2 Column2 Column2 Column3 Column4 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 You get the idea. From previous advice, I think the issue may be looping, but I am not sure. Any advice would be great. The code is below: private void table(){ String[] colName = { "Course", "Examiner", "Moderator", "Semester Available ", "Associated Programs", "Associated Majors"}; DefaultTableModel model = new DefaultTableModel(colName,0); for(Object item : courseList){ Object[] row = new Object[6]; // String[] row = new String[6]; row[0] = fileManage.getCourseList(); row[1] = fileManage.getNameList(); row[2] = fileManage.getModeratorList(); row[3] = fileManage.getSemesterList(); row[4] = fileManage.getProgramList(); row[5] = fileManage.getMajorList(); model.addRow(row); textArea = new JTable(model); } This is the class that has the arraylists: import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.Scanner; public class FileIOManagement { private ArrayList<String> nameList = new ArrayList<String>(); private ArrayList<String> courseList = new ArrayList<String>(); private ArrayList<String> semesterList = new ArrayList<String>(); private ArrayList<String> moderatorList = new ArrayList<String>(); private ArrayList<String> programList = new ArrayList<String>(); private ArrayList<String> majorList = new ArrayList<String>(); public ArrayList<String> getNameList(){ return this.nameList; } public ArrayList<String> getCourseList(){ return this.courseList; } public ArrayList<String> getSemesterList(){ return this.semesterList; } public ArrayList<String> getModeratorList(){ return this.moderatorList; } public ArrayList<String> getProgramList(){ return this.programList; } public ArrayList<String> getMajorList(){ return this.majorList; } public void setNameList(ArrayList<String> nameList){ this.nameList = nameList; } public void setCourseList(ArrayList<String> courseList){ this.courseList = courseList; } public void setSemesterList(ArrayList<String> semesterList){ this.semesterList = semesterList; } public void setModeratorList(ArrayList<String> moderatorList){ this.moderatorList = moderatorList; } public void setProgramList(ArrayList<String> programList){ this.programList = programList; } public void setMajorList(ArrayList<String> majorList){ this.majorList = majorList; } public FileIOManagement(){ setNameList(new ArrayList<String>()); setCourseList(new ArrayList<String>()); setSemesterList(new ArrayList<String>()); setModeratorList(new ArrayList<String>()); setProgramList(new ArrayList<String>()); setMajorList(new ArrayList<String>()); readTextFile(); getNameList(); getCourseList(); } private void readTextFile(){ try{ Scanner scan = new Scanner(new File("Course.txt")); while(scan.hasNextLine()){ String line = scan.nextLine(); String[] tokens = line.split("~"); String course = tokens[0].trim(); String examiner = tokens[1].trim(); String moderator = tokens[2].trim(); String semester = tokens[3].trim(); String program = tokens[4].trim(); String major = tokens[5].trim(); courseList.add(course); semesterList.add(semester); nameList.add(examiner); moderatorList.add(moderator); programList.add(program); majorList.add(major); HashSet hs = new HashSet(); hs.addAll(nameList); nameList.clear(); nameList.addAll(hs); Collections.sort(nameList); } scan.close(); } catch (FileNotFoundException e){ e.printStackTrace(); } } } This is the class where I need to have the JTable: import java.awt.*; import javax.swing.*; import java.io.*; import javax.swing.border.EmptyBorder; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import javax.swing.table.DefaultTableModel; public class AllDataGUI extends JFrame{ private JButton saveCloseBtn = new JButton("Save Changes and Close"); private JButton closeButton = new JButton("Exit Without Saving"); private JFrame frame=new JFrame("Viewing All Program Details"); private final FileIOManagement fileManage = new FileIOManagement(); private ArrayList<String> nameList = new ArrayList(); private ArrayList<String> courseList = new ArrayList(); private ArrayList<String> semesterList = new ArrayList(); private ArrayList<String> moderatorList = new ArrayList(); private ArrayList<String> majorList = new ArrayList(); private ArrayList<String> programList = new ArrayList(); private JTable textArea; public ArrayList<String> getNameList(){ return this.nameList; } public ArrayList<String> getCourseList(){ return this.courseList; } public ArrayList<String> getSemesterList(){ return this.semesterList; } public ArrayList<String> getModeratorList(){ return this.moderatorList; } public ArrayList<String> getProgramList(){ return this.programList; } public ArrayList<String> getMajorList(){ return this.majorList; } public void setNameList(ArrayList<String> nameList){ this.nameList = nameList; } public void setCourseList(ArrayList<String> courseList){ this.courseList = courseList; } public void setSemesterList(ArrayList<String> semesterList){ this.semesterList = semesterList; } public void setModeratorList(ArrayList<String> moderatorList){ this.moderatorList = moderatorList; } public void setProgramList(ArrayList<String> programList){ this.programList = programList; } public void setMajorList(ArrayList<String> majorList){ this.majorList = majorList; } public AllDataGUI(){ getData(); table(); panels(); } public Object getValueAt(int rowIndex, int columnIndex) { String[] token = nameList.get(rowIndex).split(","); return token[columnIndex]; } private void table(){ String[] colName = { "Course", "Examiner", "Moderator", "Semester Available ", "Associated Programs", "Associated Majors"}; DefaultTableModel model = new DefaultTableModel(colName,0); for(Object item : courseList){ Object[] row = new Object[6]; // String[] row = new String[6]; row[0] = fileManage.getCourseList(); row[1] = fileManage.getNameList(); row[2] = fileManage.getModeratorList(); row[3] = fileManage.getSemesterList(); row[4] = fileManage.getProgramList(); row[5] = fileManage.getMajorList(); model.addRow(row); textArea = new JTable(model); // String END_OF_LINE = ","; // // String[] colName = { "Course", "Examiner", "Moderator", "Semester Available ", "Associated Programs", "Associated Majors"}; //// textArea.getTableHeader().setBackground(Color.WHITE); //// textArea.getTableHeader().setForeground(Color.BLUE); // // Font Tablefont = new Font("Details", Font.BOLD, 12); // // textArea.getTableHeader().setFont(Tablefont); // Object[][] object = new Object[100][100]; // int i = 0; // if (fileManage.size() != 0) { // for (fileManage book : fileManage) { // object[i][0] = fileManage.getCourseList(); // object[i][1] = fileManage.getNameList(); // object[i][2] = fileManage.getModeratorList(); // object[i][3] = fileManage.getSemesterList(); // object[i][4] = fileManage.getProgramList(); // object[i][5] = fileManage.getMajorList(); // // textArea = new JTable(object, colName); // } // } } } public void getData(){ nameList = fileManage.getNameList(); courseList = fileManage.getCourseList(); semesterList = fileManage.getSemesterList(); moderatorList = fileManage.getModeratorList(); majorList = fileManage.getMajorList(); programList = fileManage.getProgramList(); // textArea.(write()); } private JButton getCloseButton(){ return closeButton; } private void panels(){ JPanel panel = new JPanel(new GridLayout(1,1)); panel.setBorder(new EmptyBorder(5, 5, 5, 5)); JPanel rightPanel = new JPanel(new GridLayout(15,0,10,10)); rightPanel.setBorder(new EmptyBorder(15, 5, 5, 10)); JScrollPane scrollBarForTextArea=new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); panel.add(scrollBarForTextArea); frame.add(panel); frame.getContentPane().add(rightPanel,BorderLayout.EAST); rightPanel.add(saveCloseBtn); rightPanel.add(closeButton); closeButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { frame.dispose(); } }); saveCloseBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //saveBtn(); frame.dispose(); } }); frame.setSize(1000, 700); frame.setVisible(true); frame.setLocationRelativeTo(null); } // private void saveBtn(){ // File file = null; // FileWriter out=null; // try { // file = new File("Course.txt"); // out = new FileWriter(file); // out.write(textArea.getText()); // out.close(); // } catch (FileNotFoundException e) { // e.printStackTrace(); // } catch (IOException e) { // e.printStackTrace(); // } // JOptionPane.showMessageDialog(this, "File Successfully Updated"); // // } }

    Read the article

  • How to bind ArrayList Object to an ASPxGridView control

    - by nikolaosk
    I have been involved with a ASP.Net project recently and I have implemented it using the awesome DevExpress ASP.Net controls. Parts of the project involved binding data from custom objects, SqlDataSource & ObjectDataSource data sources to the ASPxGridView control. In this post I will show you how to bind data from an ArrayList object to the ASPxGridView control . If you want to implement this example you need to download the trial version of these controls unless you are a licensed holder of...(read more)

    Read the article

  • java arraylist : same values to a new arraylist

    - by j0hns3n
    public static void rechnung(List<String> array) { for (int i = 0; i < array.size(); i++) { for (int j = 0; j < array.size(); j++) { if (i != j) { System.out.println(array.get(i) + " gleich " + array.get(j) + " " + (array.get(i).substring(0, 9).equals(array.get(j) .substring(0, 9)))); } } } } My Intension is to put the same values with the same date from a List in a new List. At the end I've for example three new Lists. List content: 03.07.2007 00001.tif 03.07.2007 00002.tif 18.02.2008 00003.tif 18.02.2008 00004.tif 18.02.2008 00005.tif 11.03.2009 00004.tif 11.03.2009 00005.tif

    Read the article

  • return an ArrayList method

    - by Bopeng Liu
    This is a drive method for two other classes. which i posted here http://codereview.stackexchange.com/questions/33148/book-program-with-arraylist I need some help for the private static ArrayList getAuthors(String authors) method. I am kind a beginner. so please help me finish this drive method. or give me some directions. Instruction some of the elements of the allAuthors array contain asterisks “*” between two authors names. The getAuthors method uses this asterisk as a delimiter between names to store them separately in the returned ArrayList of Strings. import java.util.ArrayList; public class LibraryDrive { public static void main(String[] args) { String[] titles = { "The Hobbit", "Acer Dumpling", "A Christmas Carol", "Marley and Me", "Building Java Programs", "Java, How to Program" }; String[] allAuthors = { "Tolkien, J.R.", "Doofus, Robert", "Dickens, Charles", "Remember, SomeoneIdont", "Reges, Stuart*Stepp, Marty", "Deitel, Paul*Deitel, Harvery" }; ArrayList<String> authors = new ArrayList<String>(); ArrayList<Book> books = new ArrayList<Book>(); for (int i = 0; i < titles.length; i++) { authors = getAuthors(allAuthors[i]); Book b = new Book(titles[i], authors); books.add(b); authors.remove(0); } Library lib = new Library(books); System.out.println(lib); lib.sort(); System.out.println(lib); } private static ArrayList<String> getAuthors(String authors) { ArrayList books = new ArrayList<String>(); // need help here. return books; } }

    Read the article

  • how do i return arraylist from a function?

    - by KoolKabin
    Hi guys, I learnt example from msdn to populate a listbox control with arraylist. http://msdn.microsoft.com/en-us/library/1818w7we(v=VS.100).aspx I want to create a function which will give return the USStates arraylist and use the returned value as datasource for listbox1 Dim USStates As New ArrayList() USStates.Add(New USState("Alabama", "AL")) USStates.Add(New USState("Washington", "WA")) USStates.Add(New USState("West Virginia", "WV")) USStates.Add(New USState("Wisconsin", "WI")) USStates.Add(New USState("Wyoming", "WY")) ListBox1.DataSource = USStates ListBox1.DisplayMember = "LongName" ListBox1.ValueMember = "ShortName I tried creating a function like: Public Shared Function FillList() As ArrayList() Dim USStates As New ArrayList() USStates.Add(New USState("Alabama", "AL")) USStates.Add(New USState("Washington", "WA")) USStates.Add(New USState("West Virginia", "WV")) USStates.Add(New USState("Wisconsin", "WI")) USStates.Add(New USState("Wyoming", "WY")) return usstates end function but it says error: Value of type 'System.Collections.ArrayList' cannot be converted to '1-dimensional array of System.Collections.ArrayList'.

    Read the article

  • C# ArrayList calling on a constructor class

    - by EvanRyan
    I'm aware that an ArrayList is probably not the way to go with this particular situation, but humor me and help me lose this headache. I have a constructor class like follows: class Peoples { public string LastName; public string FirstName; public Peoples(string lastName, string firstName) { LastName = lastName; FirstName = firstName; } } And I'm trying to build an ArrayList to build a collection by calling on this constructor. However, I can't seem to find a way to build the ArrayList properly when I use this constructor. I have figured it out with an Array, but not an ArrayList. I have been messing with this to try to build my ArrayList: ArrayList people = new ArrayList(); people[0] = new Peoples("Bar", "Foo"); people[1] = new Peoples("Quirk", "Baz"); people[2] = new Peopls("Get", "Gad"); My indexing is apparently out of range according to the exception I get.

    Read the article

  • Passing parametrized ArrayList to a function in java

    - by user150505
    I have the following fucntion. func(ArrayList <String> name) { ........ } The function fills the ArrayList. (I don't want to return the ArrayList) However, in the caller function the ArrayList obtained has all items of ArrayList as null. For eg. 1 name = new ArrayList[num]; 2 func(name); 3 System.out.println(name[0]); I get NullPointerException at line 3. Is this because of line 1, i.e. I am not parametrizing? If yes, is there another way this can be done? Because java does not allow creating a generic array of parametrized ArrayList.

    Read the article

  • putting numbers in an array into arraylist

    - by arash
    i have numbers that user enter in textBox3 and i converted them to an array nums now i want to put half of them in arraylist A and half of them in arraylist B how can i do that?thanks string[] source = textBox3.Text.Split(','); int[] nums = new int[source.Length]; for (int i = 0; i < source.Length; i++) { nums[i] = Convert.ToInt32(source[i]); } ArrayList A = new ArrayList(); ArrayList B = new ArrayList();

    Read the article

  • Is an ArrayList automaticaly declared static ,if it is an instance variable?(Java)

    - by Alex
    Hy ,what i`m trying to do is something like this: private class aClass { private ArrayList idProd; aClass(ArrayList prd) { this.idProd=new ArrayList(prd); } public ArrayList getIdProd() { return this.idProd; } } So if i have multiple instances of ArrayLIst (st1 ,st2 ,st3) and I want to make new objects of aClass : { aClass obj1,obj2,obj3; obj1=new aClass(st1); obj2=new aClass(st2); obj3=new aClass(st3); }why all of the aClass objects will return st3 if I access the method getIdProd() for each of them(obj1..obj3)? is an arraylist as a instance variable automatically declared static?

    Read the article

  • .NET BinarySearch() on ArrayList of custom objects

    - by Alex
    Hi. I have an ArrayList of custom objects that have the following properties: FileName FilePath CurrentFolder TopLevelFolder I then need to do a BinarySearch (or some other quick search) on the FileName property on all the objects in the ArrayList in .NET. In other words, I need to find the object in the ArrayList with the same FileName as the one I'm searching on. Syntax for the ArrayList's BinarySearch is this; but how do you do this for an object's property in the arraylist? public static void FindMyObject( ArrayList myList, Object myObject ) { int myIndex=myList.BinarySearch( myObject ); if ( myIndex < 0 ) Console.WriteLine( "The object to search for ({0}) is not found. The next larger object is at index {1}.", myObject, ~myIndex ); else Console.WriteLine( "The object to search for ({0}) is at index {1}.", myObject, myIndex ); }

    Read the article

  • Sorting ArrayList - IndexOutOfBoundsException -Java

    - by FILIaS
    I'm trying to sort an ArrayList with strings(PlayersNames) and imageIcons(PlayersIcons) based on the values i store in an other arrayList with integers(results). As you can see i get an indexOutOfBoundsException but i cant understand why. Maybe the earling of the morning makes me not to see plain things. ArrayList<String> PlayersNames=new ArrayList<String>; ArrayList<ImageIcon> PlayersIcons=new ArrayList<ImageIcons>; public void sortPlayers(ArrayList<Integer> results){ String tmp; ImageIcon tmp2; for (int i=0; i<PlayersNames.size(); i++) { for (int j=PlayersNames.size(); j>i; j--) { if (results.get(i) < results.get(i+1) ) { //IndexOutOfBoundsException! tmp=PlayersNames.get(i+1); PlayersNames.set(i+1,PlayersNames.get(i)); PlayersNames.set(i,tmp); tmp2=PlayersIcons.get(i+1); PlayersIcons.set(i+1,PlayersIcons.get(i)); PlayersIcons.set(i,tmp2); } } } }

    Read the article

  • How does Java handle ArrayList refrerences and assignments?

    - by Jonathan
    Hey all- I mostly write in C but am using Java for this project. I want to know what Java is doing here under the hood. ArrayList<Integer> prevRow, currRow; currRow = new ArrayList<Integer>(); for(i =0; i < numRows; i++){ prevRow = currRow; currRow.clear(); currRow.addAll(aBunchOfItems); } Is the prevRow = currRow line copying the list or does prevRow now point to the same list as currRow? If prevRow points to the same list as currRow, I should create a new ArrayList instead of clearing.... private ArrayList<Integer> someFunction(ArrayList<Integer> l){ Collections.sort(l); return l; } main(){ ArrayList<Integer> list = new ArrayList<Integer>(Integer(3), Integer(2), Integer(1)); list = someFunction(list); //Option 1 someFunction(list); //Option 2 } In a similar question, do Option 1 and Option 2 do the same thing in the above code? Thanks- Jonathan

    Read the article

  • GWT + JDO + ArrayList

    - by dvieira
    Hi, I'm getting a Null ArrayList in a program i'm developing. For testing purposes I created this really small example that still has the same problem. I already tried diferent Primary Keys, but the problem persists. Any ideas or suggestions? 1-Employee class @PersistenceCapable(identityType = IdentityType.APPLICATION) public class Employee { @PrimaryKey @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) @Extension(vendorName="datanucleus", key="gae.encoded-pk", value="true") private String key; @Persistent private ArrayList<String> nicks; public Employee(ArrayList<String> nicks) { this.setNicks(nicks); } public String getKey() { return key; } public void setNicks(ArrayList<String> nicks) { this.nicks = nicks; } public ArrayList<String> getNicks() { return nicks; } } 2-EmployeeService public class BookServiceImpl extends RemoteServiceServlet implements EmployeeService { public void addEmployee(){ ArrayList<String> nicks = new ArrayList<String>(); nicks.add("name1"); nicks.add("name2"); Employee employee = new Employee(nicks); PersistenceManager pm = PMF.get().getPersistenceManager(); try { pm.makePersistent(employee); } finally { pm.close(); } } /** * @return * @throws NotLoggedInException * @gwt.typeArgs <Employee> */ public Collection<Employee> getEmployees() { PersistenceManager pm = getPersistenceManager(); try { Query q = pm.newQuery("SELECT FROM " + Employee.class.getName()); Collection<Employee> list = pm.detachCopyAll((Collection<Employee>)q.execute()); return list; } finally { pm.close(); } } }

    Read the article

  • How to set arrayList size as null?

    - by Jessy
    String a []= {null,null,null,null,null}; //add array to arraylist ArrayList<Object> choice = new ArrayList<Object>(Arrays.asList(a)); System.out.println(choice.size()); Why the size of the arrayList choice is 5 when all the elements have been set to null

    Read the article

  • Adding instances of a class to an Arraylist in java

    - by Olga
    I have 10 instances of the class movie which I wish to add to an Arraylist named Catalogue1 in a class containing a main method I write the following ArrayList catalogue1= new ArrayList () //the class movie is defined in another class Movie movie1= new Movie () Movie movie2= new Movie () Catalogue.Add (1, movie1) What is wrong? Should I define somewhere what kind of Objects this arraylist named catalogue should contain? Thank you in advance

    Read the article

  • Cloning java ArrayList and preventing it from modifications

    - by user222164
    I have a data structure like a database Rowset, which has got Rows and Rows have Columns. I need to initialize a Columns with null values, current code is to loop thru each column for a row and initialize values to NULL. Which is very inefficient if you have 100s or rows and 10s of column. So instead I am keeping a initialized ArrayList of columns are RowSet level, and then doing a clone of this Arraylist for individual rows, as I believe clone() is faster than looping thru each element. row.columnsValues = rowsset.NullArrayList.clone() Problem with this is NullArrayList can be accidentally modified after being cloned, thus sacrificing the integrity of ArrayList at RowSet level, to prevent I am doing 3 things 1) Delcaring ArrayList as final 2) Any elements I insert are final or null 3) Methods thurough this arrayList are passed to other arrays are declared a final. Sounds like a plan, do you see any holes ?

    Read the article

  • mockito ArrayList<String> problem...

    - by Sardathrion
    I have a method that I am trying to unit test. This method takes a parameter as an ArrayList and does things with it. The mock I am trying to define is: ArrayList<String> mocked = mock(ArrayList.class); which gives a [unchecked] unchecked conversion" warning. ArrayList<String> mocked = mock(ArrayList<String>.class); gives me an error. Anyone care to enlighten me as to what I am doing wrong?

    Read the article

  • Correct way to add objects to an ArrayList

    - by ninjasense
    I am trying to add an object to an arraylist but when I view the results of the array list, it keeps adding the same object over and over to the arraylist. I was wondering what the correct way to implement this would be. public static ArrayList<Person> parsePeople(String responseData) { ArrayList<Person> People = new ArrayList<Person>(); try { JSONArray jsonPeople = new JSONArray(responseData); if (!jsonPeople.isNull(0)) { for (int i = 0; i < jsonPeople.length(); i++) { Person.add(new Person(jsonPeople.getJSONObject(i))); } } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { } return People; } I have double checked my JSONArray data and made sure they are not duplicates. It seems to keep adding the first object over and over.

    Read the article

  • Array of ArrayList Java

    - by David Bobo
    Hi, I am creating an PriorityQueue with multiple queues. I am using an Array to store the multiple ArrayLists that make up my different PriorityQueues. Here is what I have for my constructor so far: ArrayList<ProcessRecord> pq; ArrayList[] arrayQ; MultiList(){ arrayQ = new ArrayList[9]; pq = new ArrayList<ProcessRecord>(); } The problem comes when I am trying to get the size of the entire array, that is the sum of the sizes of each ArrayList in the array. public int getSize(){ int size = 0; for(int i = 1; i <=9; i++){ size = size + this.arrayQ[i].size(); } return size; } is not seeming to work. Am I declaring the Array of ArrayList correctly? I keep getting an error saying that this.arrayQ[i].size() is not a method. (the .size() being the problem) Thanks for any help! David

    Read the article

  • Compare new Integer Objects in ArrayList Question

    - by thechiman
    I am storing Integer objects representing an index of objects I want to track. Later in my code I want to check to see if a particular object's index corresponds to one of those Integers I stored earlier. I am doing this by creating an ArrayList and creating a new Integer from the index of a for loop: ArrayList<Integer> courseselectItems = new ArrayList(); //Find the course elements that are within a courseselect element and add their indicies to the ArrayList for(int i=0; i<numberElementsInNodeList; i++) { if (nodeList.item(i).getParentNode().getNodeName().equals("courseselect")) { courseselectItems.add(new Integer(i)); } } I then want to check later if the ArrayList contains a particular index: //Cycle through the namedNodeMap array to find each of the course codes for(int i=0; i<numberElementsInNodeList; i++) { if(!courseselectItems.contains(new Integer(i))) { //Do Stuff } } My question is, when I create a new Integer by using new Integer(i) will I be able to compare integers using ArrayList.contains()? That is to say, when I create a new object using new Integer(i), will that be the same as the previously created Integer object if the int value used to create them are the same? I hope I didn't make this too unclear. Thanks for the help!

    Read the article

  • Prevent duplicate entries in arraylist

    - by timyh
    Say I create some object class like so public class thing { private String name; private Integer num; public oDetails (String a, Integer b) { name = a; num = b; } ...gets/ sets/ etc Now I want to create an arraylist to hold a number of this object class like so. ArrayList<thing> myList = new ArrayList<thing>; thing first = new thing("Star Wars", 3); thing second = new thing("Star Wars", 1); myList.add(first); myList.add(second); I would like to include some sort of logic so that in this case...when we try and add object "second" rather than add a new object to the arrayList, we add second.getNum() to first.getNum(). So if you were to iterate through the ArrayList it would be "Star Wars", 4 I am having trouble coming up with an elegant way of handling this. And as the arraylist grows, searching through it to determine if there are duplicate name items becomes cumbersome. Can anyone provide some guidance on this?

    Read the article

  • Java: ArrayList bottleneck

    - by Jack
    Hello, while profiling a java application that calculates hierarchical clustering of thousands of elements I realized that ArrayList.get occupies like half of the CPU needed in the clusterization part of the execution. The algorithm searches the two more similar elements (so it is O(n*(n+1)/2) ), here's the pseudo code: int currentMax = 0.0f for (int i = 0 to n) for (int j = i to n) get content i-th and j-th if their similarity > currentMax update currentMax merge the two clusters So effectively there are a lot of ArrayList.get involved. Is there a faster way? I though that since ArrayList should be a linear array of references it should be the quickest way and maybe I can't do anything since there are simple too many gets.. but maybe I'm wrong. I don't think using a HashMap could work since I need to get them all on every iteration and map.values() should be backed by an ArrayList anyway.. Otherwise should I try other collection libraries that are more optimized? Like google's one, or apache one.. Thanks

    Read the article

1 2 3 4 5 6 7 8 9 10 11 12  | Next Page >