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 have the following issue:
I have got an "array" Like this:
[0] = This
[1] = is
[2] = my array
when "array.remove(0)" - the array is changing to
[0] = is
[1] = my array
but I don't want to lose their positions (index)...
what can I do here? they shall stay like:
[0] = null
[1] = is
[2] = my array
You can simply set array element of required position to null, it will do the job:
array[pos]=null;
Instead of calling remove(0), you can use arrayList.add(0, null). This will do the job for you.
You can't.
java arrays start at index zero and have consecutive indices (natural positive numbers).
There are two basic approaches:
replace the "hole" values by placeholder items.
A natural value with java would be null.
With strings you might also use an empty string.
Use a map
There you can apply any logic you like to the indices.
Related
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
}
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 3 years ago.
Improve this question
I am working to make an Instagram type project where you can follow up to five people. For my follow method I tried to loop through a string array (length 5) and say if 'user getting followed' isn't on the list of followers, add that person to the array. But when I do instead of displaying the array like this (when I call the Arrays.toString()):
follows: [user 1, user 2, user 3, user 4, user 5]
It displays the array like this:
follows: [user1user2user3user4user5, , , , ]
Here is my code I am talking about:
for (int i = 0; i < 5; i++) {
if(...) {
this.following[i] += gettingFollowed.getHandle();
/* getHandle() gets the name of the user getting followed and adds it to the string array, 'following' */
this.following[i] += gettingFollowed.getHandle();
The += instruction appends to a String - which is why you are seeing your output. You are appending all your users to the same index.
this.following[i] = gettingFollowed.getHandle();
Simply remove the '+' and you are now setting the value.
Still, without knowing what .getHandle() does - this might not completely fix your code.
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};
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
So here is an example text file of what I need to store into a 2d array, and print it out in certain ways.
the file contains two numbers followed by text, the numbers represent the number of rows and columns.
4 4
SOME
FILE
WITH
TEXT
So I already stored them into an appropriate 2d array, I am just not sure how to print it out to fit this pattern:
THEE
XTLM
EIIO
TWFS
so far to print out the 2d array: i have the code:
for(int i=0;i<row;i++)
{
for(int m=0;m<column;m++)
{
System.out.print(charArray[i][m])
}
System.out.println();
}
I know to flip it vertically, and horizontally, by manipulating the parameters of how it is printed, but how do I manipulate the parameters so that it will print like the above? THanks so much
The text file would look exactly like above, called txt1.txt.... and I am going to use 4 of them. IT looks exactly like the example listed above:
4 4
Some
File
With
Text
my goal is to just store them in a 2d array, and manipulate how I print them. I already know how to manipulate it in some ways but just not in the pattern also listed above. THanks guys! :)
You want to start with the last element in the last column, and go backwards by columns first. So all you need to do is change the direction and order of your loops:
for(int m=column - 1;m>=0;m--) {
for(int i=row - 1;i>=0;i--) {
System.out.print(charArray[i][m])
}
System.out.println();
}
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