Related
I ran into an issue I can't seem to solve, and all the searches I do are not completely relevant to the issue I am having, and trying to implement those things to solve my issue still doesn't work. I've spent an hour trying to find another question or post somewhere that would help but can't seem to find any specific to my issue (unless Google just doesn't want to work today).
I am trying to create a method that returns an array of all of the odd numbers between 1 and n, say in this example 1 to 255.
I tried the following (here is the method currently):
import java.util.Arrays;
public class BasicJava: {
public Integer[] arrayOfOdds() {
int n = 255;
Integer[] odds = new Integer[(n+1)/2];
for(int i = 1; i < n+1; i+=2) {
odds[i/2] = i;
}
return odds;
}
}
Main Method:
import java.util.Arrays;
public class BasicJavaTest {
public static void main(String[] args) {
BasicJava test = new BasicJava();
System.out.println(Arrays.toString(test.arrayOfOdds()));
}
}
I tried using an array to do the same thing before switching to using an ArrayList (I like other data structures more than I do arrays) and converting to an array and got the same output (I will just put part of the output array to not use too much space):
[0, 1, 0, 3, 0, 5, 0, 7, 0, 9, 0, 11, 0, 13, 0, 15, 0, 17, 0, 19, 0, 21, 0, 23, 0, 25, 0, 27, 0, 29, 0, 31, 0]
What do I need to resolve this issue?
If I just wanted to print all of the odds between 1 and N using the same for loop and if statement, I would get the correct output.
Thank you
You can do this is linear time complexity and without using an ArrayList.
Your final output will always have n/2 elements so your array size can be fixed at the same. In the next step you can simply populate the values in your array.
FYR code:
int[] arr = new int[((n+1)/2)];
for(int i = 0, e = 1; i < arr.length; e += 2, i++) {
arr[i] = e;
}
You can use IntStream like this.
static int[] arrayOfOdds() {
return IntStream.iterate(1, i -> i + 2)
.takeWhile(i -> i < 256)
.toArray();
}
public static void main(String[] args) {
System.out.println(Arrays.toString(arrayOfOdds()));
}
output:
[1, 3, 5, 7, 9, 11, 13, 15, ... , 251, 253, 255]
int N = 255;
Integer[] array = new Integer[(N+1)/2];
for (int j = 1; j < N+1; j+=2) {
array[j/2] = j;
}
System.out.println(Arrays.toString(array));
You don't need to add condition to check for each integer.
Here is a simple trick to create odds number:
Start with 1 and increase 2 for next element. ( 1,3,5...)
public static void main(String[] args) {
List<Integer> arr = getOddList(255);
System.out.print(arr);
}
private static List<Integer> getOddList(int n) {
List<Integer> nums = new ArrayList<>();
for (int i = 1; i < n; i = i + 2) {
nums.add(i);
}
return nums;
}
//Output [1, 3, 5, 7, 9, 11...
I have a homework assignment that needs me to write a method that, given a list, will remove all multiples of 3 and add the first 5 multiples of 4, 5, and 6. It will then add that list to a HashSet, removing the duplicate integers, and return the size of the HashSet.
I've figured out everything else in this problem save for the "add first 5 multiples of 4, 5, and 6". My current code is the one I'm stuck with, using a for loop to iterate from 1 to 30. However, given an empty list, this adds 28, which is the 7th multiple of 4. I've tried nesting loops so I can iterate to 30 while at the same time iterating to 5, but none of my attempts have worked. Can anyone help me out?
Below is my current code.
public static int modify(List<Integer> list) {
for (int i = 0; i < list.size(); i++) {
if (list.get(i) == null) {
throw new IllegalArgumentException("Cannot be null.");
}
if (list.get(i) % 3 == 0) {
list.remove(i);
}
}
for (int i = 1; i <= 30; i++) {
if (i % 4 == 0) {
list.add(i);
}
if (i % 5 == 0) {
list.add(i);
}
if (i % 6 == 0) {
list.add(i);
}
}
Collections.sort(list);
HashSet<Integer> unique = new HashSet<Integer>();
unique.addAll(list);
return unique.size();
}
Instead of counting to 30 and checking for incidences of multiples of 4, 5, and 6, why don't you find the multiples directly?
for (int i = 1; i <= 5; i++) {
list.add(4 * i);
list.add(5 * i);
list.add(6 * i);
}
If there are any duplicates, they'll be removed when you add them to the HashSet.
Side note: I'm not sure why you're bothering to sort the list before you add them to the HashSet. Sets inherently have no order so it doesn't matter if the list is sorted or not.
I'm assuming you're expecting the output of passing in an empty list to be:
unique = [4, 5, 6, 8, 10, 12, 15, 16, 18, 20, 24, 25, 30]
size = 13
With your current logic, there's nothing stopping it from adding multiples which are more than 5 (i.e. the 7th multiple of 4 for example), you're just continuing on until you've hit 30 in the loop. Instead, I might recommend having your for loop go 1-5 and then multiply by the factor you'd like a multiple of. i.e.:
// add the first 5 multiples of 4,5,6 to the list
for (int i = 1; i < 6; i++) {
list.add(i*4);
list.add(i*5);
list.add(i*6);
}
I rewrote the whole method making some changes:
The removal part of multiples of 3 in this way is more efficient. I
used the very useful removeIf() method.
I wrote the solution to your problem, identical to the other
answers.
CODE
public static void main(String[] args) {
List<Integer> list = new LinkedList<>();
for (int i = 0; i < 30; i++) {
list.add(i);
}
System.out.println(modify(list));
}
public static int modify(List<Integer> list) {
list.removeIf(n -> (n % 3 == 0));
for (int i = 1; i < 6; i++) {
list.add(4 * i);
list.add(5 * i);
list.add(6 * i);
}
Collections.sort(list);
HashSet<Integer> unique = new HashSet<>(list);
System.out.println(unique.toString()); // You can delete this
return unique.size();
}
OUTPUT
[1, 2, 4, 5, 6, 7, 8, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 22, 23, 24, 25, 26, 28, 29, 30]
26
//other arrays sequences sample
{23,45,38, 9,43,25,18}
{21,33,22, 5, 1,44,16}
{28,24, 5,42,15,49,41}
{43,18,50,29,22,32,25}
{33,38,27,35,25, 1,12}
{21,33,22, 5, 1,44,16}
{28,24, 5,42,15,49,41}
I have a little project where i have a list of sequences and i would like to have them rearranged from small to large and reprinted for use to find repeating sequences and show how many time each sequence repeats
Now I wrote a small code sorting the sequences from small to large with the aid of a friend in order to solve the first problem.
But I would like it to work with multiple sets and arrange each set and print them on there own line, In order for me to find the repeating patterns
Yet every time i change the {int} to accommodate more sequences the code fails
public class Main {
public static void main(String args[]) {
int x[] = {37, 36, 20, 23, 44, 27, 24};
for (int y = 0; y <= x.length; y++) {
for (int z = 0; z <= x.length - 2; z++) {
if (x[z] > x[z + 1]) {
int temp = 0;
temp = x[z];
x[z] = x[z + 1];
x[z + 1] = temp;
}
}
}
for (int y = 0; y < x.length; y++) {
System.out.print(x[y]);
}
}
}
as part of the second problem finding the repeating sequence i'm looking into dis post and busy reworking to see if it will work.
Yet as far as i see it will find the repeating number and not sequence witch is where i'm stuck.
How to find repeating sequence of Integers in an array of Integers?
I'm still new to java and any aid will be appreciated
Kind Regards
Deon
can't you just use Arrays.sort(x) to sort the arrays in to order.
I suggest you split this in two separate questions.
I will now try to answer to the sorting problem, however, I'm not quite sure if I understand your problem here.
You implemented the bubble sort. You can see in the linked Wikipedia article how this is correctly implemented. Basically there is a do-while loop as the outer loop.
Alternatively, you could also use Java's Arrays#sort
EDIT
You can create a two dimensional array for your sequences like this:
int[][] seq = {
{23,45,38, 9,43,25,18},
{21,33,22, 5, 1,44,16},
{28,24, 5,42,15,49,41},
{43,18,50,29,22,32,25},
{33,38,27,35,25, 1,12},
{21,33,22, 5, 1,44,16},
{28,24, 5,42,15,49,41}
};
Then, you can iterate over seq and sort each contained array individually:
for(int i = 0; i < seq.length; i++) {
int[] x = seq[i];
// sort x
}
Thanks Hage, It's small but I learned a lot Here is the working code
public class Main {
public static void main(String[] args) {
int[][] seq={{ 42, 22, 40, 1, 11, 5, 43 },
{ 13, 11, 18, 45, 3, 44, 19 },
{ 46, 1, 32, 47, 35, 7, 36 },
{ 48, 21, 38, 29, 3, 12, 11 },
};
for (int i=0; i < seq.length; i++) {
int[] x=seq[i];
Arrays.sort(x);
for (int number : x) {
System.out.print(number + " ");}
System.out.print("\n");
}
}
}
I'm making a method for printing out the contents of an array, but when I run it it gives an error. Can you take a look at my code? I can't find the problem
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
for (int i = 0; i < arr.length(); i++)
{
System.out.println(arr[i]);
}
length is a field in arrays, not a method. Remove the parenthesis:
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
The proper way to get the length of the array is arr.length, not arr.length()
I want to make an array from something like this:
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
To this:
{6, 5, 4, 3, 2, 1, 0, 13, 12, 11, 10, 9, 8, 7, 20, 19, 18, 17, 16, 15, 14}
But I'm clueless how. I think this method can serve as a good alternative to the code I plan to do. (I'm using Dr. Java so no imported files BTW)
for an integer array called integer[]:
(for j = 0; j < 3; j++) {
(for k = 0; k = 6; k++) {
int newj = j+1;
int array = integer[k*newj];
integer [k*newj] = integer[6 - k*newj -1];
integer[6 - k*newj - 1] = array;
}
}
But this doesn't work.
Any advice? It's not part of an assignment, but it's part of an exam that will happen within a week and I want to be sure of this.
There are 21 elements in your array. From the description you mentioned, you want to seperate it into 3 parts. Each part has 7 elements and reverse them.
For the each part, we can do the swap data opearation.
Swap the 1st element with the 7th element.
Sawp the 2nd element with the 6th elememt.
Swap the 3rd element with the 5th element.
... ...
Note: The end condition is 7/2 for data swap. It is the middle index of the 7 elements.
Here one more thing is to determine what is the start index and end index for each divided part.
Thefollowing code is working for your requirement. Hope this can help you some.
for (int j = 0; j <3; j++) {
for (int k = 0; k <7/2; k++) {
int newj = j+1;
int array = integer[7*newj-k-1];
integer[7*newj-k-1]= integer [7*j+k];
integer [7*j+k] = array;
}
}