Printing indices of an array [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 7 years ago.
Improve this question
I'm having trouble figuring out why my j won't print out with the current boolean statement. Its only if I put != will it then print 100,000-1 indices.
public static void main(String[] args) {
//array size and name
double[] largearr;
largearr = new double[100000];
double index = 0.00149;
int j = 0;
//population of array and the printout of the elements
for(int i = 0; i < 100000; i++) {
Arrays.fill(largearr, index);
index += 0.00149;
// System.out.println(largearr[i] + " ");
}
for(j = 0; j < largearr.length; j++) {
if(largearr[j] == 58.00122999995376) {
System.out.println("j");
}
}
}

Because you don't have that value in any position of your largerarr. The first for loop in your code fills all the elements of largearr array with the newly incremented index value, and it does it 100000 times: at the end you'll have largearr filled with the last iteration's value of index, that surely isn't 58.00122999995376. So the second for loop doesn't enter the if check for any value and the program quits without printing anything.
If you could explain what are you trying to achieve maybe we could help you better.

Related

Why foreach loop gives array elements but for loop gives address instead of elements? 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 12 days ago.
Improve this question
I tried to print all the elements in an array using both for loop and foreach loop.
In for loop, I got addresses of elements instead of elements themselves. But by using for loop I got the elements themselves. So how this is working even I didn't override toString method also but am getting elements!!
public class ArrayReturn {
public static int[] Multi(int []a)
{
for (int i = 0; i < a.length; i++) {
a[i] = a[i]*2;
}
return a;
}
public static void main(String[] args) {
int ar[] = {2,3,4,5,6,7};
int z[] = Multi(ar);
for (int i = 0; i < z.length; i++) {
System.out.println(z);
}
for (int i : z) {
System.out.println(i);
}
}
}
OUTPUT
[I#5a07e868
[I#5a07e868
[I#5a07e868
[I#5a07e868
[I#5a07e868
[I#5a07e868
4
6
8
10
12
14
I expected either address from both loops or elements. But I got address in for loop and elements in foreach loop.
As others have told you, in your for loop you're printing the array z, so for that reason it's printing the object address what you're seeing. If you want to print the value stored in each position, you have to use the for loop's index.
for (int i = 0; i < z.length; i++) {
System.out.println(z[i]);
}

Find longest squential streak in arraylist java [closed]

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 2 years ago.
Improve this question
I am trying to write a code in java where suppose i have an arraylist of (1,3,4,5,8,9,11,12,13,14,15)
i need the code to analyze the longest streak of consecutive numbers in the arraylist. From the above arraylist 11,12,13,14,15 is the longest streak so the code should give this in the output in form of another arraylist.
the code should be executed on any ArrayList consisting of different numbers
actually, i have made the code where i have come up to the point of extracting index of postive values from a bigger arraylist and stored it in another arraylist. Now, im stuck at finding the longest streak of consecutive numbers in the newly made arraylist.
Here is my code:
List<Integer> mega = new ArrayList<Integer>();
for (int i = 0; i <= data.size() - 1; i++) {
double a = (data.get(i).adjClose) - (data.get(i).open); //calculating a value
if (a > 0) { // if value is bigger than zero, then store the index of that element in a new arraylist
mega.add(i);
}
}
ArrayList<Integer> bigger = new ArrayList<>();
for (int x = 0; x < numbers.size(); x++) {
int current = numbers.get(x);
ArrayList<Integer> temp = new ArrayList<>();
temp.add(current);
for (int y = x + 1; y < numbers.size(); y++) {
int nextValue = numbers.get(y);
if (nextValue == current + 1) {
temp.add(nextValue);
current = nextValue;
}
else {
break;
}
}
if (temp.size() > bigger.size()) {
bigger.clear();
bigger.addAll(temp);
}
}
numbers is the ArraList of your numbers and
Inside bigger is your sequence.

Finding longest array in a three-dimensional array and returning a one-dimensional array that contains the largest array [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 3 years ago.
Improve this question
A method public static int[] largestArray(int[][][]a)
returns a single dimensional array that contains the largest array in the 3D array
If you wish to get the first element which matches your condition, you can try this code.
for(int i = 0; i < arr.length; i++) {
int[][] arrI = arr[i];
for(int j = 0; j < arrI.length; j++) {
int[] arrJ = arr[j];
if(arrJ.length > saved.length) {
saved = arrJ;
}
}
}
Hope that helps.

Traverse the array and each time you encounter and change the value in java [closed]

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 3 years ago.
Improve this question
if Take an array of size 10 and take user inputs.how can i Traverse the array and each time you encounter '6' or '7', replace them with 60 and 70 respectively in java??
Maybe you can try this,
int[] input = {1,2,3,4,5,6,7,8,9};
for(int i = 0; i < input.length; i++)
if(input[i]==6 || input[i]==7)
input[i]*=10;
//Printing
for(int i : input)
System.out.println(i);
Here is a simplistic approach. Loop through the array and check each positions value. If it's a 6 or 7 replace as expected.
public static void main(String[] args){
int[] array = {1,2,3,4,5,6,7,8,9};
for(int i = 0; i < array.length; i++){
if(array[i] == 6){
array[i] = 60;
}
else if(array[i] == 7){
array[i] = 70;
}
}
for(int i : array){
System.out.println(i);
}
}

I have a loop that displays 10 values: 1.0, 1.1,1.2, etc. how can I put those values into an array? [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 6 years ago.
Improve this question
I have a loop that displays 10 values: 1.0, 1.1, 1.2, etc.:
int i = 0;
int x = 10;
for(i;i>x;i++)
{
System.out.println(x);
}
but instead of displaying the values, I want to put them in an array. How do I do that?
How about:
// You want x ints.
int x = 10;
// Make an array big enough to hold x ints.
int[] array = new int[x];
// Loop x times.
for(int i = 0; i < x; i++) {
// Put the next number into the array.
array[i] = i;
}
first your way in writing for loop is need to be more clean
it should :
for(int i=0; i > x; i++){
System.out.println(x);
}
second your boolean condition in for loop isn't true because x=10 is always bigger than i=0 so it won't print any thing.
third to put the values in array :
simply define array : int[] numbers = new int[size of array];
then put each value inside the index i of array :
numbers[i] = i;
finally for loop will be like:
for(int i=0; i < x; i++){
numbers[i] = i;
}

Categories

Resources