arraylist

Removing items from ArrayList upon collision (Java)?

只谈情不闲聊 提交于 2019-12-24 05:21:28
问题 As a preface, I have searched the forums but found nothing relating to my specific situation. I just started learning Java about a week ago, and this is my first foray into object oriented programming. I'm building a basic game (think somewhat like Space Invaders when it comes to mechanics). I have a "Projectile" class, a "FallingThings" class (which is the parent class to the classes I have for objects falling down (Money, Friend, Enemy)). Each projectile that is shot is stored in an

Removing an object from a arraylist

只谈情不闲聊 提交于 2019-12-24 05:09:33
问题 I have an arraylist with names, phone numbers and locations. I would like to remove certain items from that array if possible Is there anyway to remove the item once I have found it like I have tried below? public void delete(String nameToDelete) { for (Entry entry : Directory.entries) { if (entry.name.equalsIgnoreCase(nameToDelete)) { //remove(entry); } } } Thanks 回答1: Reason? Iterators returned by ArrayList is fail-fast in nature. The iterators returned by this class's iterator and

Shifting the contents of the `ArrayList` to the right

≯℡__Kan透↙ 提交于 2019-12-24 04:32:12
问题 I've written a code which shifts the contents of the ArrayList to the right and the shifting can be any number passed to the method shiftRight(String myString, int shift) Inside the method, I need to put every char of a passed String into the ArrayList myList . For example, if I have "abcdef", 2 the result should be efabcd as it shifts 2 letters to the right. Another example: "abcdef", 4 then the output is cdefab My code gives edabcf when the shift is 2 and the String is "abcdef" whereas it

Shifting the contents of the `ArrayList` to the right

混江龙づ霸主 提交于 2019-12-24 04:31:13
问题 I've written a code which shifts the contents of the ArrayList to the right and the shifting can be any number passed to the method shiftRight(String myString, int shift) Inside the method, I need to put every char of a passed String into the ArrayList myList . For example, if I have "abcdef", 2 the result should be efabcd as it shifts 2 letters to the right. Another example: "abcdef", 4 then the output is cdefab My code gives edabcf when the shift is 2 and the String is "abcdef" whereas it

How to copy elements from an ArrayList to another one NOT by reference?

谁都会走 提交于 2019-12-24 04:23:25
问题 I'm trying to copy each element from one ArrayList (av) to another one (copia). The thing is that they're copied by reference, so whenever I make any changes in the original one, the copy is modified as well. Of course, this behavior is not wanted. How should I write this method? public void copiarArrayList(ArrayList<Articulo_Venta> copia, ArrayList<Articulo_Venta> av){ copia.clear(); for (int i = 0; i < av.size(); i++) { copia.add(av.get(i)); } } Articulo_Venta has these fields: int codigo;

How to draw a diamond shape in java?

最后都变了- 提交于 2019-12-24 04:22:35
问题 So i have to draw a diamond shape. Not a Static diamond but a diamond that i will myself drag and draw. I've used General Path to do it but it is drawing a diamond that is not straight; the diamond is bend to the left and it's not being drawn to where my mouse is pointed. Here is my code to create the diamond shape. Can someone please help me solve this? private GeneralPath drawDiamond(int x1, int y1, int x2, int y2){ int x = Math.min(x1, x2); int y = Math.min(y1, y2); // Gets the difference

Error: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList

落花浮王杯 提交于 2019-12-24 03:50:15
问题 I have a class like this: public class Questionnaire { private String questionnaireLibelle; private List<Question> questions; // ... } And I've initialized a new Questionnaire like this: Questionnaire q = new Questionnaire( "Architecture des ordinateurs", Arrays.asList( new Question( "1. La partie du processeur spécialisée pour les calculs est :"), new Question("2. Dans un ordinateur, les données sont présentées par un signal électrique de la forme :"), new Question("3. Les différents

Appending Integer array elements in Java

天大地大妈咪最大 提交于 2019-12-24 03:37:12
问题 I have an array, say int a[]={2,0,1,0,1,1,0,2,1,1,1,0,1,0,1}; I need to append each of the 5 neighboring elements and assign them to a new array b with length=(a.length/5); and i want to append the 5 neighboring elements so that I have: int b[]={20101,10211,10101}; I need to do this for various length arrays, in most cases with length of a being greater than 15. Any help would be greatly appreciated, I'm programming in Java. Thanks in advance. 回答1: It's pretty straighforward: // Assuming a

Java Object reference

风流意气都作罢 提交于 2019-12-24 03:23:38
问题 This is regarding the usage of ArrayList returned by another class instance variable. Class A { //assigned list of string to it. private List < String > newAl; //returns the list public List < String > getList() { return newA1; } } Class Test { public void go() { List < String > list = a.getList(); list.add(""); } } In the Test class when i retreive list and manipulate the list.Because of the reference ,class A list also got manipulated.If A is part of third party code.How do I correct my

How elements are stored in a collection?

♀尐吖头ヾ 提交于 2019-12-24 03:13:03
问题 I am familiar with Java collections are implemented and I am trying to understand how collections are implemented in Scala. Do Scala's collections ultimately wrap Java collections? What is the equivalent of ArrayList in Scala? ArrayList in Java contains a variable elementData that holds an array [*]. Is this implementation similar in the equivalent class/trait in Scala? How growable collections in Scala are implemented? Kind regards, Alexandre [*] http://grepcode.com/file/repository.grepcode