Java Arrays Assistance [closed] - java

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 7 years ago.
Improve this question
I have the following 2 array codes.
int max = 100;
int length = 50;
String [] cars = new String[length];
int [] nums = new int [max];
I have 2 questions.
What is the value of nums[6]?
And,
What is the value of nums[max] ?

From the Java Language Spec:
Each class variable, instance variable, or array component is initialized with a default value when it is created (§15.9, §15.10)
For type int, the default value is zero, that is, 0.
So the answer to your first question is 0.
In Java, array indexes start with 0. Thus array has array.length elements with indexes 0, 1, 2, ... ,array.length - 1, and array[array.length] would throw an ArrayOutOfBoundsException.

nums[6] will be 0 because the int array would hold default values.
nums[max] will throw an exception, because your array's length equals max.

I hope this answers your question. nuns[6] is 0, because its the default value that java gives to arrays that haven't been specified.

Related

Remove element from array, without reindexing [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 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.

Digital (number) length in 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 7 years ago.
Improve this question
How to get the length of some number in Java?
Length of string is string.length(), but what is it for an integer?
I tried this:
int lengthNumber = (String.valueOf(maxNumber)).length();
But it always returns 1, for every number.
Try
Integer#toString().length();
For Example
Integer a=222;
int length=a.toString().length();
Output
3
When I ran this:
int Number = 100003;
int lengthNumber = (String.valueOf(Number)).length();
System.out.println(lengthNumber);
My output was 6, indicating that it works correctly.
Just make sure that your variables are declared properly.
This method will work if the above isn't working.
int x = 100003;
String y = "" + x;
Now you can use y.length(). Printing y gives 100003, and printing y.length() gives 6.
System.out.println(y);
System.out.println(y.length());

How to counter the number of strings exceeding a certain number of characters in a string array. Java [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 get how to count the number of words in a string by extinguishing the spaces between words, but am confused of how to approach it when it asks for strings equal to or exceed a certain number of characters.
Complete the countLongWords() method below, which takes an
array of Strings as its argument. This method returns an integer value
representing the number of Strings in the array that are 5 or more characters
long.
pseudocode
int count = 0
for str in stringArray
if str.length() >=5
count++
return count
You can convert this to java
Try to always think what you will do if you have a paper with this array of Strings written on it and want to count the number of Strings that satisfied this property.
Then it's very easy to translate that in a computer program.
Create a variable to hold the number of Strings with a length superior than 5
Loop through the element of your array
For each element, if it holds the property increment this variable
Return the variable
int number = 0;
for(int i = 0; i<array.length; i++){
if(array[i].length()>4){
number++;
}
}
return number;

2D Array and a loop within an interval [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 need to crate a loop which sums the arrays values which are within the interval (for instance 2-5). My main problem is getting from the first checked value of the array to the next one and so on. Thankyou in advance.
int x=0,y=0,s=0;
int[][] myArray = { {0,1,2,3}, {3,2,1,0}, {3,5,6,1}, {3,8,3,4} };
Scanner scan = new Scanner(System.in);
int b = scan.nextInt();//lowest value
int c = scan.nextInt(); //highest value
if (myArray[x][y]>b || myArray[x][y]<c)
s=s+myArray[x][y]
//then check next one
if (myArray[x][y]>b || myArray[x][y]<c)
s=s+myArray[x][y]
You need to put these in a nested for-loop. Otherwise, they are executed only once, with x=0 and y=0. So all you are doing is essentially:
if (myArray[0][0]>b || myArray[0][0]<c)
s=s+myArray[0][0]
So s could only possibly be 0 or the first element in the 2D array.
Now, this conditional:
if (myArray[x][y]>b || myArray[x][y]<c)
does not mean what you think it means. This would evaluate to true for all numbers as long as b is less than c. For the semantics you are looking for, you want an AND operator (&&).

How do you make an array that has 1-3 int, instead of the default 0-3 int? [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
How do you make an array that has 1-3 int, instead of the default 0-3 int?
like
int[] anArray;
anArray = new int[3];
has numbers 0-3 I want index starting at 1
You make an array of four elements, and then never use the '0' element.
Sorry, but you can't change the language.
The index starts at 0. You'll just need to set The variable different in your loops if it helps readability
Instead of for I=0; i
You'd use
For I= 1; I
Would recommend you get use to indexing from zero though.
Apologies for typos. Using phone
You don't, arrays in Java start their indices at 0.
And new int[3] does not have numbers 0-3, it has indices 0-2.
Java doesn't allow this. In most programming languages, array indexes start from zero. And in the cases where they start from one, you can't change the lower bound.
The only main-stream1 language I can think of where you could specify both the lower and upper bounds of an array is Ada. (And recent versions of Fortran too ...)
1 ... and calling Ada mainstream these days is "a stretch".

Categories