Task: Given an integer denoting the size of the array.Fill array with integers.
Return true,if the array contains three of same elements not next to each other.
Return false,if the array not contains three of same elements,or contains but next to each other.
public static boolean noThreeInRow(int [] array){
for(int i = 0; i < array.length-1; i++){
if(array.length < 3) System.exit(0);
if(array[i] != array[i+1]){
return true;
}else return false;
}return true;
}
I can't solved this problem.Someone could help me?I am very beginner!
try using a counter and two loops to compare each number.
int counter = 0;
for(int i = 0; i < array.length-1; i++){
if(array.length < 3) System.exit(0);
for (int j = 0; j < array.length-1; j++){
if (array[i] == array [j+2]) counter ++
}
return true
}return true;
Try something like that. But you will have to figure out how to deal with the numbers before array[i] during the j loop. Happy Coding :)
Related
So I made this sortion sort method, all using for loops.
I revised it on white board over and over, it looks perfect, however, when I implemented it, it keeps giving me wrong sort, but if I reversed the if condition, it will give me the right answer but in reverse, this doesn't make sense!
public void insertionSort(){
for (int i = 1; i < items.length; i++){
for (int j = 0; j < i; j++){
if (items[i] < items[j]) {
int temp = items[i];
shift(items,j, i-1);
items[j] = temp;
}
}
}
}
private void shift(int[] array, int index1, int index2){
if (index1 == index2)
array[index1 + 1] = array[index1];
else{
for (int i = index2; i >= index1; i--)
array[i+1] = array[i];
}
}
Thanks for the input, I discovered the problem in my Array class, it simply doubles the array size, my mistake was that I used the array.length instead of count.
public void insertionSort(){
for (int i = 1; i < count; i++){
for (int j = 0; j < i; j++){
if (items[i] < items[j]) {
int temp = items[i];
shift(items,j, i-1);
items[j] = temp;
}
}
}
}
I'm looping through a 2d array. To check if the first row elements are the same/equal. I'm having an issue pulling this off. Below is what I have so far.
public void checkMatch(Values[][] val){
//TODO check elements in 2d array for matches
for(int i = 0; i < val.length; i++){
for(int j = 1; j < val[i].length; j++){
if(val[i][0].equals(val[i][1]) && val[i][0].equals(val[i][2])){
System.out.println("Match");
}else {
System.out.println("No Match");
}
}
}
}
Figured it out.
public void checkMatchRows(Values[][] val){
for(int i = 0; i < val.length; i++){
for(int j = 1; j < val[j].length -1; j++){
if(val[i][0] == val[i][1] && val[i][1] == val[i][2]){
System.out.println("Match");
}else {
System.out.println("No Match");
}
}
}
}
This question already has answers here:
How do i find and count duplicates in a 2 dimensional array?
(4 answers)
Closed 5 years ago.
I am a beginner coder just learning arrays. I recently learned 2d arrays and I am trying to find out if there is a duplicate in a 2d array. I know how to check if there is a duplicate in the same column or row, but I cannot figure out how to compare a number if it is not in the same column or row as the number I am trying to compare it to. Here is my code as of now:
public static boolean correctNumbers(int[][] values) {
for (int i = 0; i < values.length; i++) {
for (int j = 0; j < values[i].length; j++) {
int num = values[i][j];
for (int k = j + 1; k < values.length; k++) {
if (num == values[i][k] || num > values.length * values.length || num < 1) {
return false;
}
}
for (int l = i + 1; l < values.length; l++) {
if (num == values[l][j] || num > values.length * values.length || num < 1) {
return false;
}
}
}
}
return true;
}
I need to create a method and I cannot use any other methods in creating it.
Thanks for the help!
Edit: It returns false if there is duplicates in the array, a number in the array is less than 1, or greater than total number of elements in the array. In other words this method is checking to see if the array contains all the values of 1 to (i*j) in the array. I realized I did a bad job of explaining that part. Thanks again!
You can simplify the logic by saving the numbers into a hashset and when you're iterating them check each one if it's already there (meaning duplicate) otherwise add it:
public static boolean correctNumbers(int[][] values) {
Set<Integer> set = new HashSet<>();
for (int i = 0; i < values.length; i++) {
for (int j = 0; j < values[i].length; j++) {
int num = values[i][j];
if (set.contains(num)) {
return false;
}
set.add(num);
}
}
return true;
}
EDIT
We can use an array for the same functionality the hashset is doing in the code above:
public static boolean correctNumbers(int[][] values) {
int n = values.length;
int[] dict = new int[n * n + 1];
for (int i = 0; i < n; i++) {
for (int j = 0; j < values[i].length; j++) {
int num = values[i][j];
if (num < 1 || num > n || dict[num] != 0) {
return false;
}
dict[num] = 1;
}
}
return true;
}
I'm trying to evaluate a hand to see if they have a pair and I feel like this is right but i keep getting errors. Any idea on what im doing wrong?
public boolean isPair() {
String[] values = new String[5];
int counter = 0;
//Put each cards numeric value into array
for(int i = 0; i < cards.length; i++){
values[i] = cards[i].toString();
}
//Loop through the values. Compare each value to all values
//If exactly two matches are made - return true
for(int j = 0; j < values.length; j++){
for(int k = 0; k < cards.length; k++){
if(values[j].equals(cards[k].toString()))
counter++;
if(counter == 2)
return true;
}
counter = 0;
}
return false;
}
The first obvious error I can see, is that you re comparing the first card with itself here :
for(int j = 0; j < values.length; j++){
for(int k = 0; k < cards.length; k++){
Your index k should not be verified if it equals j.
Secondly, why are you using a your variable "hand" in the comparison when you bothered to create a array of String containing your hand ?
values[j].equals(cards[k].toString())
You can write :
values[j].equals(values[k])
I dont think it is responsible for any errors, but it s much easier to understand.
And finally, your counter is false. A pair, is by definition two cards of the same value. So, you have to check if a value is present only two times(that means 1 equality) in your hand.
So you'll have :
for(int j = 0; j < values.length; j++){
for(int k = 0; k < cards.length; k++){
if(k!=j){
if(values[j].equals(values[k]))
counter ++;
}
}
if (counter ==1 ) //Counter==1 means the a value matched with an other value in your hand only once, involving one pair.
return true;
else counter = 0;
}
return false;
String[] values = new String[5];
should be
String[] values = new String[cards.length];
However, even better would be to not use values anyway, since it's almost a copy of cards.
Basically, I am given an array of numbers and I have to count all the negative numbers.
Then make a new array that contains all of the positive numbers from the previous with the length of the array being firstarray-numberOfNegatives
Here is my code:
public void removeNegatives()
{
int numberOfNegative = 0;
for(int i = 0; i < numbers.length-1; i++)
{
if (numbers[i] < 0) numberOfNegative++;
}
int [] numbers2 = new int[numbers.length-numberOfNegative];
int count = 0;
for(int i = 0; i < numbers2.length; i++)
{
if (numbers[count] > 0) numbers2[i] = numbers[count];
count++;
System.out.println(numbers2[i]);
}
numbers = numbers2;
}
I am getting the wrong result: negative numbers are replaced with 0s
At first write i < numbers.length; or i <= numbers.length-1; instead of i < numbers.length-1;
And then fix code.
Also note that you can have zeros in your 'numbers' array, so in if() in first for() you should write <=0 instead of <0
for(int i = 0; i < numbers.length; i++)
{
if (numbers[i] > 0) numbers2[count] = numbers[i];
count++;
System.out.println(numbers2[count-1]);
}
Firstly: you have an off-by-one error in your first for loop. Walk through some small example arrays in your head or on paper and you'll see.
Secondly: I think you are using your two index counters backwards in the second section. The count is supposed to be used in your new array, and i in your old one.
public void removeNegatives()
{
int numberOfNegative = 0;
for(int i = 0; i < numbers.length; i++) //
{
if (numbers[i] < 0) numberOfNegative++;
}
int [] numbers2 = new int[numbers.length-numberOfNegative];
int count = 0;
for(int i = 0; i < numbers.length; i++)
{
if (numbers[i] >= 0) numbers2[count++] = numbers[i]; //
}
numbers = numbers2;
}
Run through a debugger and watch this line...
That -1 is suspicious
for(int i = 0; i < numbers.length-1; i++)
:)
Need to reverse your index variables in the second pass. Also the first loop had a length-1 instead of length. The second loop should go over the full length of the original array not the resulting array.
public void removeNegatives()
{
int numberOfNegative = 0;
for(int i = 0; i < numbers.length; i++)
if (numbers[i] < 0)
numberOfNegative++;
int [] numbers2 = new int[numbers.length-numberOfNegative];
int count = 0;
for(int i = 0; i < numbers.length; i++)
{
if (numbers[i] >= 0) {
numbers2[count] = numbers[i];
System.out.println(numbers2[count]);
count++;
}
}
numbers = numbers2;
}
In
for(int i = 0; i < numbers2.length; i++)
{
if (numbers[count] > 0) numbers2[i] = numbers[count];
count++;
System.out.println(numbers2[i]);
}
numbers = numbers2;
}
You are iterating as many times as numbers2 has positions, however, you increase i at every pass (in the for loop) regardless of whether you fund a positive number or not. Your output arrray would therefore have the first numbers2.length positive integers of numbers interleaved with a bunch of 0s!