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.
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 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;
}
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 6 years ago.
Improve this question
i need to fill this array with 500000 int numbers this my code
public class lab_02 {
public static void main(String[] args) {
// 500000 Array
Array array = new Array (500000);
}
}
i don't know what should i do use loop or Recursion
thank you
1) If you don't need duplicates use a Set
Set<Integer> intSet = new HashSet<Integer>();
2) Then you can add your random generated int add it to the Set
while (intSet.size() < DESIRED_SIZE) {
//generate the randomInteger - DESIRED_SIZE is 500k in your case
intSet.add(randomInteger);
}
The tricky part here is ensuring uniqueness. Checking if an item is unique can be expensive. Since the OP does not state what the bounds on the random numbers are I am assuming min 1 and max 500000 for this solution.
One approach would be to enter the numbers sequentially 0 to 500000 and then shuffle the array.
int size = 500000;
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < size; i++) {
list.add(i);
}
Collections.shuffle(list);
for (int i = 0; i < size; i++) {
System.out.println(list.get(i));
}
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.
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
Can someone explain to me how to store the previous two fibbonnaci numbers it would help alot in this problem.
public static void main(String[] args) {
int k = 0;
for (int x = 1; x < 13; x++) {
if (k > 2) {
k = (k - 1) + (k - 2);
}
System.out.print(k+" ");
k++;
}
}
when you got number 5 as the printed out put you will set k++ , that will make k=6.
after that k = (k - 1) + (k - 2); output k = (6-1)+(6-2) = 5+4 = 9 , (note : the next should be 8 so your algorithm is wrong)
You have mistaken the Idea of Fibonacci numbers.
the nth Fibonacci number is equal to the sum of previous two Fibonacci numbers. not to the (Fn-1)+(Fn-2)
Edited :
So as you can see if we know the first 2 Fibonacci numbers we can calculate the third by adding those two. and the fourth one will be the summation of second one and third one and it goes ..... to n.
Okay here is a way that you don't need a recursive approach ( you need to store the found Fibonacci numbers in an Array)
okay assume you want to find first n Fibonacci numbers. then create an array of size n and set first and second elements to one (1) since first two Fibonacci numbers are 1 and 1. now loop through the array from 2 to n. at each iteration add the previous two element to the next element.
go through the code. you will find it very easy to do.
public static void fib(int n){
int Fibonacci [] = new int[n];
Fibonacci [0]=1;
Fibonacci [1]=1;
for (int i = 2; i < n; i++) {
Fibonacci [i]=Fibonacci [i-1]+Fibonacci [i-2];
}
System.out.println(Arrays.toString(Fibonacci ));
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
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
Improve this question
array[0] = 1;
array[1] = 2;
array[2] = 3;
array[3] = 4;
items_arr = 4;
System.out.println("The elements in the array are: ");
for (int x = 0; x < items_arr; x++)
System.out.println("Array[" + x + "]=" + array[x]);
System.out.print("\n");
Scanner insert = new Scanner(System.in);
System.out.print("Enter an Element to Insert: ");
int input = insert.nextInt();
for (s = 0; s < items_arr; s++)
if (array[s] == input)
break;
items_arr++;
for (s = 0; s < items_arr; s++)
System.out.println("Array[" + s + "]=" + array[s]);
break;
The output is. The elements are
Array [0]= 1
Array [1]= 2
Array [2]= 3
Array [3]= 4
Enter an element to Insert: 5
Array [0]= 1
Array [1]= 2
Array [2]= 3
Array [3]= 4
Array [4]= 0
when I insert 5 it posts 0
any suggestions please.. thanks!
To insert in to the array you shuould be doing follwoing operation
array[s]=input
Two notes here
Arrays are fixed length, and you should be checking the array length before inserting values in to that,other wise you will get ArrayIndexOBException. Safer to sue List/Set
As better coding practise, and to improve the readablity, you should be enclosing the conditional/loop statements (such as if or for) - see eg below
eg: 1
for (int x = 0;x<items_arr;x++) {
System.out.println("Array["+x+"]="+array[x]);
}
eg 2:
for(int s = 0; s < items_arr; s++) {
if (array[s] == input) {
break;
}
}
You have not inserted 5 in your array,
do something after items_arr++
array[ items_arr] = input;
If you do not insert any thing then by default every element is 0
You should be using a Collection type; I would recommend an ArrayList - that is -
List<Integer> al = new ArrayList<Integer>();
for (int i = 1; i < 5; i++) {
al.add(i);
}
Scanner insert = new Scanner(System.in);
System.out.print("Enter an Element to Insert: ");
int input = insert.nextInt();
al.add(input); // And so on...
You are not updating/inserting the array with the new input.
for(s = 0; s < items_arr; s++)
if (array[s] == input)
break;
items_arr++;
just replace the above code with
array[ items_arr] = input;
items_arr++;