Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
class blabla extends JPanel
{
public blabla()
{
//code
}
}
class Main
{
public static void main(String[] args)
{
JPanel b;
ArrayList<blabla> c;
blabla a = new blabla();
b = new JPanel();
c = new ArrayList<blabla>();
b.add(a);
c.add(a);
blabla d = (blabla) b.getComponent(0);
System.out.println(c.indexOf(d));
}
}
Are ArrayList a and JPanel a same objects?
What should be the codes output?
This answer is based upon what you mentioned in the question when no code snippet was provided and quetion was...
I have created a component instance, drawn it onto the screen, and added it to an ArrayList.
I'm accessing it by referencing to the drawn one using it's children (getParent() method). However, when I then pass this reference to ArrayLists indexOf(); method, it returns -1.
I suppose that means that the component does not exist in the ArrayList.
Is this what should happen, or did I probably mess something up in my program? I'm NOT providing you with a SSCCE, I'm not asking you to do any coding, just to tell me if this is normal Java behavior...
Here goes the my response
The javadoc of indexOf() says...
Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.
As you can see this depends on the equals() implementation for you component. Check your implementation as that holds the key of retrieving the value from list.
Alright here goes the answer for your modified question...
Are ArrayList a and JPanel a same objects?
No. But they contain the same object of class blabla.
What should be the codes output?
The output is
0
which is right as you placed the same component in the JPanel and ArrayList and 0 is the index of element.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
Is it possible to get the last value of an element inside a list with many different values?
List<EventDTO> eventDto = new ArrayList<>();
Inside this list there are three elements: id(Integer), name(String), and lastStatus(String)
I'm trying to compare a String to see if it's equal to the last value stored in the element lastStatus inside the list. As the event is updated, many values are stored in said element. I'm trying to get the last one stored.
String originalStatus = 'Pending';
How can I access the list, find the element lastStatus, and see if the last value stored inside of it equals to originalStatus ?
In accordance to Java 8 here https://docs.oracle.com/javase/8/docs/api/java/util/List.html, you could use the function
E get(int index)
to access last element in a list. It returns the element at the specified position in this list.
Example: eventDto.get(eventDto.size() -1 )
while using the function
int size()
to get the last index of the list.
For comparison, you can use the function
boolean equals(Object o) of String class on the the adequate attribut.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have tried to take input in Java language through the vector method. I have tried these two different methods and just want to know the difference between:
Vector v = new Vector();
v.addElement(new Integer(10));
vs
v.add(10);
addElement and add are equivalent, although the return types differ.
However in your case the overall behaviour will be different due to your writing new Integer(10) in one case and 1 in the other case.
In the former case, a new Integer is created with the value 10 and a reference to that is pushed onto the container.
In the latter case, 1 will is auto-boxed to an Integer. But its value just happens to be within the range of interned integers (-128 to +127). So no new Integer is actually created, but a reference to one of the interned integers is pushed onto the container.
There is no difference in function betweenaddElement() and add(), except that add() returns a boolean.
From the JavaDoc for addElement():
Adds the specified component to the end of this vector, increasing its size by one. The capacity of this vector is increased if its size becomes greater than its capacity.
This method is identical in functionality to the add(E) method (which is part of the List interface).
In relation to the boolean that is returned by add(), this is defined in the JavaDoc as returning true if the collection has been changed by the method.
Put another way, this returns true if the element was added to the vector, and false otherwise.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I´m trying to implement a way to find all possible solutions to Pentominos of W, L and I with the help of DLX-Algorithm, or Algorithm-X of Knuth in a 5 x n rectangle.
My approach is to find all ways to insert a W into a 5 x 3 rectangle. I want to implement this first and then the full program.
So this is tons of stuff to read and to understand and I understood somehow how it works but I encountered a problem when looking at this part of the code given by my prof.
class Node // represents 1 element or header
{
Node C; // reference to column-header
Node L,R,U,D; // left, right, up, down
Node()
{
C=L=R=U=D=this; // supports circular lists
}
}
My question: How does the reference to C=...=this work? I know the difference between instance and local variables but I don't know how to understand the reference to "this" in the constructor. What does it do?
In this instruction
C=L=R=U=D=this;
thisrepresents the instance of the Node class that is being processed at runtime (dynamically): so it simply means that all your other Node variable will receieve reference to the current instance of Node that was being constructed.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am learning the basics of lists in Java and I was wondering what .set did I already understand the concept of .add however I cant really find anything about .set and its relation to lists other then examples. I would really appreciate if someone could give me some insight on this command.
Set will specify the position in the List for the object you are storing. The List inteface represents an ordered collection of objects so the position is able to be changed. Similar to an array.
Look at the section Positional Access and Search Operators on: http://docs.oracle.com/javase/tutorial/collections/interfaces/list.html
set(pos, elem) as per the Java docs:
Replaces the element at the specified position in this list with the
specified element.
This means that you can change the stored element/reference at a specific position in the list, as long as the position is within the allowed position bounds. So, if you have 3 elements within the list already, you can specify position in set(position,element) to a value between 0 and 2, inclusive. Here is a simple demonstration of how you can replace the 1th (so really the 2nd, as it's 0-indexed) element in an ArrayList and then set it back to the original value:
import java.util.ArrayList;
public class Foo {
public static void main(String[] args) throws Exception {
ArrayList<Integer> foo = new ArrayList<Integer>();
foo.add(1);
foo.add(1);
foo.add(2);
foo.add(3);
System.out.println(foo);
foo.set(1, 999);
System.out.println(foo);
foo.set(1, 1);
System.out.println(foo);
}
}
But really, this is explained more than clearly enough in the Java doc for List, so as others have said: read it and try it out next time.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have one existing code, where the written line is
private Rule[] ruleList;
Where Rule is a CLASS.
I want to remove all Rules which are added here in ruleList.
but as I click right it shows like this
If it is a List, how can I delete these rules from ruleList?
Thanks
The ruleList is an array and not a List. Thus you will not find a clear method.
But you can use
Arrays.fill(ruleList, null);
You have an array and not a list. Solution :
ruleList = new Rule[ruleList.length];
To remove all items from a List use clear()
If you want to remove all items in an array from a List use the remove method.
Example
public class Rules {
public static void main(String[] args) {
Rule[] rules = new Rule[2];
rules[0] = new Rules.Rule();
rules[1] = new Rules.Rule();
//Scenario 1
List<Rule> ruleList = new ArrayList<Rule>();
ruleList.add(Arrays.asList(rules)); //adds array to list
ruleList.clear(); //removes all items from List
//Scenario2
ruleList.add(Arrays.asList(rules)); //adds array to list
ruleList.remove(Arrays.asList(rules)); //remove all rules in [] form list
//Scenario3
Arrays.fill(rules, null); //removes all elements in array
}
static class Rule{
}
}
It is important to determine whether you are using a List or an Array. The provided code depicts an array, however the verbiage keeps referring to a List.
If you actually have a List and want to remove all elements see Scenario 1 in the example.
If you actually have a List and want to remove all elements in the array from the List see Scenario 2 in the example.
If you actually have array and want to remove all elements from the array see Scenario 3 in the example.