arraylist

How to sort a two-dimension ArrayList

半腔热情 提交于 2019-12-22 06:39:36
问题 I have a two-dimension ArrayList that contains double values: ArrayList<ArrayList<Double>> data = new ArrayList<ArrayList<Double>>(); In analogy with classic arrays , I would like to sort the "cols" of this matrix :I want to take the items having the same index in the sub ArrayLists, and then sort them. Like calling Collections.sort() for every column... By rows I mean the outer level and inner level are columns. What is the proper way to do this? I thought about iterating over the matrix to

Retrieving data from a VB.NET arraylist of objects

拥有回忆 提交于 2019-12-22 05:05:07
问题 I am trying to retrieve the correct value from an ArrayList of objects (.NET 1.1 Framework): I have the following defined: Public AlList As New ArrayList Public Class ItemInfo Public ItemNo As Int16 Public ItemType As String Public Reports As Array Public PDFs As Array End Class The form_load event code contains: Dim AnItemObj As New ItemInfo Then a loop that includes: AnItemObj.ItemNo = AFile.RecordId AnItemObj.ItemType = temp AlList.Add(AnItemObj) So I should now have an ArrayList of these

ArrayList of integer arrays in Java

情到浓时终转凉″ 提交于 2019-12-22 05:00:10
问题 everyone. I'm just getting into Java, and I'm trying to write a simple game where an enemy chases the player on a grid. I'm using the simple algorithm for pathfinding from the Wikipedia page on pathfinding. This involves creating two lists with each list item containing 3 integers. Here's test code I'm trying out to build and display such a list. When I run the following code, it prints out the same numbers for each array in the ArrayList. Why does it do this? public class ListTest { public

How to update JComboBox content from ArrayList?

我是研究僧i 提交于 2019-12-22 04:50:51
问题 I have JComboBox based on ArrayList: private ArrayList<String> klienci = new ArrayList<String>(); private JComboBox klienciLista; and I add it in constructor: klienciLista = new JComboBox(klienci.toArray()); klienciLista.setPrototypeDisplayValue("#############################"); panel.add(klienciLista); //JPanel panel At the start List is empty. Client gets via socket new ArrayList in thread: public void run() { try { host = InetAddress.getLocalHost().getHostName(); socket = new Socket(host,

Sort an (Array)List with a specific order

允我心安 提交于 2019-12-22 04:42:38
问题 I have a list of objects and I would like to sort it with a defined order. For ex. I have an object with a field String color . I would like to sort my list on the color field so that it always has first white than blue than yellow and than all the others(if possible alph. ordered but not necessary): Before sorting: After sorting: orange white white blue green yellow brown orange yellow black black brown ... ... Is there a (easy) way to do that? EDIT: I have to add a complication more... What

How to replace a value conditionally in a Collection, such as replaceIf(Predicate<T>)?

僤鯓⒐⒋嵵緔 提交于 2019-12-22 04:09:42
问题 Is there any easy way we could replace a value in a List or Collection if the value is null? We can always do list.stream().filter(Objects::nonNull); and maybe add 0 back to the list. But what I am looking for is an API like list.replaceIf(Predicate<>) . 回答1: This will only work on a List , not on a Collection , as the latter has no notion of replacing or setting an element. But given a List , it's pretty easy to do what you want using the List.replaceAll() method: List<String> list = Arrays

how to remove blank items from ArrayList.Without removing index wise

我们两清 提交于 2019-12-22 04:08:20
问题 public class ArrayListTest { public static void main(String[] args) { ArrayList al=new ArrayList(); al.add(""); al.add("name"); al.add(""); al.add(""); al.add(4, "asd"); System.out.println(al); } } o/p [, name, , , asd] desire O/p [name,asd] 回答1: You can use removeAll(Collection<?> c) : Removes all of this collection's elements that are also contained in the specified collection al.removeAll(Arrays.asList(null,"")); This will remove all elements that are null or equals to "" in your List .

How to use gson to convert json to arraylist if the list contain different class?

独自空忆成欢 提交于 2019-12-22 03:55:14
问题 I want to store an arraylist to disk, so I use gson to convert it to string ArrayList<Animal> anim=new ArrayList<Animal>(); Cat c=new Cat(); Dog d=new Dog(); c.parentName="I am animal C"; c.subNameC="I am cat"; d.parentName="I am animal D"; d.subNameD="I am dog"; anim.add(c); anim.add(d); Gson gson=new Gson(); String json=gson.toJson(anim); public class Animal { public String parentName; } public class Cat extends Animal{ public String subNameC; } public class Dog extends Animal{ public

Returning an ArrayList from a WebService in Java

你说的曾经没有我的故事 提交于 2019-12-22 03:49:19
问题 I am having a problem returning an ArrayList from my web service (Java). I have written a test web service and client which consumes it. All appears to work fine - that is the client is calling the server and the server receives the operation request. However, I have written a simple method that I want it to return an ArrayList. I have my interface definition as follows: @WebService @SOAPBinding(style = Style.RPC) public interface ISQLServerConnectionWS { @WebMethod ArrayList

Get specific objects from ArrayList when objects were added anonymously?

▼魔方 西西 提交于 2019-12-22 03:44:39
问题 I have created a short example of my problem. I'm creating a list of objects anonymously and adding them to an ArrayList . Once items are in the ArrayList I later come back and add more information to each object within the list. Is there a way to extract a specific object from the list if you do not know its index? I know only the Object's 'name' but you cannot do a list.get(ObjectName) or anything. What is the recommended way to handle this? I'd rather not have to iterate through the entire