arraylist

How can I sort an arraylist without using collections.sort()? [duplicate]

心已入冬 提交于 2019-12-18 09:45:33
问题 This question already has answers here : sorting integers in order lowest to highest java (7 answers) Closed 5 years ago . I have been looking for a while for a way to sort an arraylist without using collections.sort as my own logic is flawed and I have been having a lot of trouble. I need to sort it in a way that I can use a method I created that basically does what collections.swap does in order to completely sort an arraylist. Here is my code: public static void mySort(ArrayList<Double>

How to convert ArrayList into JList object? java [duplicate]

丶灬走出姿态 提交于 2019-12-18 09:37:58
问题 This question already has answers here : how to bind ArrayList to JList (5 answers) Closed 3 years ago . I have an ArrayList that gets larger as more users join a chatroom. The main JFrame has a list box that displays all currently connected users. But when i try to pass the arraylist into the jframe list i get this following error: "The method setListData(Object[]) in the type JList is not applicable for the arguments (ArrayList)" Thanks for your time. 回答1: You could simply use setListData

ArrayList methods are not working

百般思念 提交于 2019-12-18 09:34:28
问题 I am learning about Java and I'm stuck with this ArrayList problem: the compiler give me error when I try to use simple methods, like add . Here is the code: public class ArrayList { public static void main(String[] args) { ArrayList list = new ArrayList(); list.add("Value A"); list.add("Value B"); list.add("Value C"); } } The method is defined in the Javadoc. It should be really simple to do it, but I really don't know what I'm doing wrong here. 回答1: You have created your own ArrayList class

Why am I getting index out of bounds exception?

点点圈 提交于 2019-12-18 09:27:56
问题 I've made this code to convert whole base 10 numbers into binary. The code I believe is everything that it needs to be but I can't get me ArrayLists to work. I've spent a few hours on this site and other trying countless changes to no avail. I've gotten the code to compile without and errors but as soon as I enter an int the program crashes. Here is the code: import java.util.Scanner; import java.util.ArrayList; public class BinaryConverter { public static void main(String[] args) { Scanner

Does a SubClass object in an ArrayList<SuperClass> keep methods unique to SubClass?

China☆狼群 提交于 2019-12-18 09:17:27
问题 Basically what the title says, but some elaboration. I have a SuperClass with a couple of SubClasses. I needed an ArrayList to hold both types of Subclasses so hence the ArrayList of type SuperClass. I tried to access Subclass1's getQuantity() method using ArrayList.get(0).getQuantity(); (assuming that index 0 is of type SubClass1). I get the error: getQuantity is undefined for the type SuperClass. Do the SubClass objects not keep their properties when put into a SuperClass ArrayList? And if

Java - How to write ArrayList Objects into txt file?

混江龙づ霸主 提交于 2019-12-18 09:00:20
问题 /** I have some methods likes add,display,sort,delete,and exit that implemented the ArrayList function. It works correctly, but the problem is that the objects that had been added were not saved on a .txt file, just the temporary objects. So I need to add them into text file,so that I can display and delete them later. Here's the part of the codes. */ public class testing { public static void main(String[] args) { String Command; int index = 0; Scanner input = new Scanner(System.in);

How to move contents of one ArrayList to another?

心不动则不痛 提交于 2019-12-18 08:52:44
问题 Is there a way to move the entire contents of an ArrayList to another instance of ArrayList in O(1)? I.e.: only the reference to the backing array is passed from one instance to the other (elements are not copied one by one). For example: ArrayList<String> a = new ArrayList<>(Arrays.asList("A", "B", "C")); ArrayList<String> b = new ArrayList<>(); a.moveContentsTo(b); // 'a' is now empty, while 'b' contains everything that 'a' did before and 'a != b' // It is desired that the 'moveContentsTo'

How to Sort 2D ArrayList<String> by Only the First Element [duplicate]

旧时模样 提交于 2019-12-18 08:26:06
问题 This question already has answers here : Sorting an ArrayList of objects using a custom sorting order (10 answers) Sorting a collection of objects [duplicate] (9 answers) Java 2D ArrayList and sorting (3 answers) Closed 6 years ago . To avoid the duplicate claims , I've looked at this post and it isn't exactly what I wanted. Every other 2D ArrayList question involved double or int numbers; my question is about Strings . What I'm doing I have a 2D ArrayList, defined like this: ArrayList

How to Sort 2D ArrayList<String> by Only the First Element [duplicate]

让人想犯罪 __ 提交于 2019-12-18 08:25:44
问题 This question already has answers here : Sorting an ArrayList of objects using a custom sorting order (10 answers) Sorting a collection of objects [duplicate] (9 answers) Java 2D ArrayList and sorting (3 answers) Closed 6 years ago . To avoid the duplicate claims , I've looked at this post and it isn't exactly what I wanted. Every other 2D ArrayList question involved double or int numbers; my question is about Strings . What I'm doing I have a 2D ArrayList, defined like this: ArrayList

Unmodifiable List in java

情到浓时终转凉″ 提交于 2019-12-18 07:36:53
问题 I'm trying to set a List unmodifiable. In my code, I have a method which returns a list. This list shouldn't be modified, but I don't want to catch the exception returned by the unmodifiableList. private List<T> listeReferenceSelectAll = null; List<T> oListeRet = new ArrayList<T>(); oListeRet = listeReferenceSelectAll; return new ArrayList<T>(oListeRet); It is an existing code and I have to transform it to return an unmodifiable list, but if an "add" method has been called, no exception has