How does the System.arraycopy method work? [closed] - java

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
A sample of the statement to type when using the method is
System.arraycopy(data, i, data, i+1, data.length-i-1);
The way it works according to my book is that the system move all element from i onward one position up. But intuitively I will think that the method will move all the element after i one position down. So there is empty space to put the copied element.
I am confused now as to how to move elements around in the same array. What does the statement really say?

The definition of System.arraycopy is
System.arraycopy(Object src, int srcPos,
Object dest, int destPos,
int length)
Therefore, this code copies
data[i .. (i + n - i - 1)] = data[i..(n-1)]
to
data[i+1 .. (i+1 + n-i-1)] = data[i+1..n]
(where the range includes the front but excludes the back, and n = data.length)
and thus leaves a "blank" spot at data[i]. Graphically:
[0] [1] [2] [3] ... [i] [i+1] [i+2] ... [n-2] [n-1]
src |-----------------------|
dest |-------------------------|

Related

Trouble obtaining average to an array [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I am trying to get an average of an array and I found this code during my research.
int myArray[] = {1,2,3};
Arrays.stream(myArray).average();
System.out.println(Arrays.toString(myArray));
When I run it, the results come up as
[1, 2, 3]
I'm new at java and I feel like there's something obvious that I'm not seeing.
You are printing the original Array.
To print the calculated array add the next line:
System.out.println(Arrays.stream(myArray).average().getAsDouble());
Output:
2.0
You are just printing only the array, Instead, you can do this.
int myArray[] = {1,2,3};
System.out.println(Arrays.toString(Arrays.stream(myArray).average().getAsDouble()));
OR
int myArray[] = { 1, 2, 3 };
OptionalDouble ans = Arrays.stream(myArray).average();
System.out.println(ans.getAsDouble());
Here,OptionalDouble is a container object which may or may not contain
a double value. If a value is present, isPresent() will return true
and getAsDouble() will return the value.

Index out of bounds error ArrayList [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
for (int i = 0; i < totalArray.size(); i++){
studentNumber.add(Long.parseLong(totalArray.get(i * 3)))
lastName.add(totalArray.get((i * 3) + 1));
firstName.add(totalArray.get((i * 3) + 2));
I'm not sure what is going on here. I've getting an index out of bounds error on this code. totalArray.size() is 42 however I'm getting the error at index 42 with the second line (parseLong).
As cricket_007 points out, on the 14th iteration of your for statement, i is 14 and your second line attempts to access element 42 of your index (where the last element of your array is accessed as element 41 because it's 0 indexed).
To stop the for statement at the proper place, merely do the inverse operations which you do on your last line of code.
i < (totalArray.size()-2)/3
(which was pointed out by Benjamin Lowry).
You're multiplying your i by 3 in your studentNumber.add(Long.parseLong(totalArray.get(i * 3))) line, so when i >= 14, it goes out of bounds.
Try setting it to studentNumber.add(Long.parseLong(totalArray.get(i))) and run it. You probably wanted to place your * 3 outside the first parenthesis, but I can't deduct your logic.
When i is 14, you are getting the index 42 with i * 3. Which is out of bounds for a list of size 42.
Your third line triples the number. This means that if i was 30 (which is possible with your current set up), then when tripled, would equal 90, which is obviously above your max size.
You need to do
for (int i = 0; i < (totalArray.size() - 2) / 3 ; i++)
In order for your code to work. Otherwise all three of your lines will exceed the max at some point.

I am getting a "not a statement" error on a for each loop [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
The problem seems to be in the increment, but I need it to decrease by 2. The "length" variable is for the length of a series of numbers
public int longMethodName()
{
int length = cardNumber.length();
longMethodName = 0
for(int i=length-1; i<0; i-2)
{
int cardNumberInt = Integer.parseInt(cardNumber.charAt(i));
int tempVar = cardNumberInt*2;
longMethodName = longMethodName + tempVar;
}
return longMethodName;
}
You need to change it to i=i-2 or i-=2 to decrement by 2.
You might be trying to emulate the i++/i-- syntax, which is simply shorthand for i = i+1 or i=i-1. However, that syntax only works for change by 1 (formally speaking ++ and -- are unary operators ), so i-2 won't work directly.
You also need to fix the other errors, as detailed in the other answer.
1.) longMethodName = 0 // Semicolon missing
2.) i-2, need to change to i = i-2
3.) Integer.parseInt(), cardNumber.charAt(i)returns char which is not allowed
You can also use i-=2 so you don't have to write i a second time ;-)

How to subtract a number(int) from a character in java [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Hi I'm trying to subtract an int from a char but I keep being told that the compiler "cannot convert from int to char".
I have tried changing the constant to a char but it didn't help.
is there any easy way to do this subtraction?
test[1] = characterArray[1] - ASCII_SUB;
Any help much would be appreciated.
The problem is that subtraction is never performed on char values in Java. Instead, both operands are promoted to int (via binary numeric promotion), and the result of the subtraction is an int as well. So you'll need to cast the result back to char:
test[1] = (char) (characterArray[1] - ASCII_SUB);

Best algorithm to remove elements between 2 given indices [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
So I'm trying to learn algorithm efficiency. I know how to remove one element from the array, but not sure how to remove between two indices. Lets pretend that the
list = {1,2,3,4,5,6,7,8,9,10}, and we call the removeBetween method with arguments:
removeBetween(2, 6);
public void removeBetween(int FirstIndex, int LastIndex)
{
}
A general algorithmic direction you can follow :
To remove all numbers between 2 given indices, say (FirstIndex, LastIndex):
Copy all elements from index 0 to FirstIndex to a result array.
Next copy all elements from LastIndex to Array.lenght()-1 indices to the same result array above.
Return result.
so if you have your items in an array list you can do something like:
ArrayList<Whatever object is> newElements = yourlist.sublist( 0, firstIndex );
newElements.addAll( yourlist.subList( LastIndex + 1, yourlist.size() ) ;

Categories

Resources