Java .set for lists? [closed] - java

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.

Related

Java: How to get the number of items in a ShortBuffer? [closed]

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 2 years ago.
Improve this question
I wonder how to get the number of items in a ShortBuffer.
I want the number of items that are really in the buffer, not the maximum capacity.
Thanks.
Buffer is not a Collection, but a (comparatively thin) wrapper around a primitive array that provides some useful methods for operating on groups of primitive values. Like a primitive array, it always contains values for each valid index.
Therefore the number of items is always equal to its capacity.
It does not keep track of which indices have already been written to since its creation. And as one major use case is to wrap an existing array while still reflecting all changes to the wrapped array, that would not even be possible to implement.
ShortBuffer holds a position that keeps track on where to put elements. You can use it to know how many elements you put in. The number of elements always equals its capacity as others mentioned.
#Test
fun testShortBuffer() {
val shortBuffer = ShortBuffer.allocate(1024)
println(shortBuffer.position()) // position 0
shortBuffer.put(1)
println(shortBuffer.position()) // position 1
shortBuffer.put(shortArrayOf(2, 3, 4))
println(shortBuffer.position()) // position 4
shortBuffer.clear()
println(shortBuffer.position()) // position 0
}

Counting the repetition of an element of an array java [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I need to count the frequency of an element in arrays.
I used the method
Collections.frequency(Arrays.asList(arr),element);
but I get zero all the times
any ideas ?!
If you are ArrayList consists of elements of custom type
example person bean, or employee object.
Make sure you have overridden equals() method and hash() methods
if you have not overridden these methods that Collection method wont work.
You need to give details about "arr" & element. However, I did came across this some time back when I tried to use an array of primitives such as int[], converting them to a List using Arrays.asList()
There is nothing like List of "int". An Integer would work however, Integer arr[] = {1,1,1,1,3,3,4,5,5,5,6};

How to remove Object in Java List? [closed]

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.

Return True if Arrays have same elements [closed]

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 9 years ago.
Improve this question
I could use some help comparing two arrays, previously created in another method. They are called enterNumbers, user input array and drawNumbers, a randomly generated array.
My method header should look like this:
public static boolean containSameElements(int[] enterNumbers, int[] drawNumbers)
{
}
The method should compare the two arrays and return true if the numbers are the same, regardless of the order.
Not looking for the answer, just maybe a place to start.
Just sort them before
Arrays.sort(enterNumbers);
Arrays.sort(drawNumbers);
if(Arrays.equals(enterNumbers, drawNumbers)){
System.out.println("both are same");
}
Well, you can either
Create two histograms (using a hash based map/set) to count elements
in each array, and then compare the sets/maps. This solution is O(n)
space and O(n) time on average. Have a look at Map or Set for this. (Either you want Map or Set depends if existence of duplicates is important or not)
Another solution is sort and iterate. This is O(nlogn) time worst
case. Have a look on Arrays.sort() for this solution.
if (drawNumbers.length != enterNumbers.length)
return false;
List<Integer> base = new ArrayList<Integer>();
for (Integer i : drawNumbers)
base.add(i);
for (Integer i : enterNumbers)
base.remove(i);
return base.isEmpty();
This is a very common "problem" which can be solved using different methods. If I understand you correctly, all of the numbers have be inside the both arrays, but they don't have to be at the same indexes?
Then you can just make a while/for loop with (with two counters; one for each array) and check if the number on index 0 in the first array equals any of the numbers in the second array. If it doesn't the while/for-loop is done and the test failed. If it does go on to the next index in the first array. Continue until everything is tested(all numbers in first array versus the second array) or until a number doesn't exist in both arrays. Good luck

References mixup in java [closed]

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.

Categories