Java - Retrieve next 50 results - java

rob = response.getJSONObject();
array = rob.getJSONArray("data");
fr = new ArrayList();
int count = array.length();
for (int i = 0; i < array.length(); i++) {
friend = array.getJSONObject(i);
fr.add(friend.get("name"));
}
Here fr is my array list.
I want to select first 50 result from output names.
Then next 50 result from output and then next 50, so on until all response ends.
Is there any way I can do that ? Itrate or For loop ?
for(int i = 0; i<fr.size(); i++){
System.out.print(fr[i]+",")
if(i%5==0) System.out.println();
}
Tried above code but result in untable some time it select one some time all .

Are you looking for this?
for (int i = 0; i < fr.size(); i++) {
System.out.print(fr[i]+",");
if ((i + 1) % 50 == 0) { // You have to use i + 1 because otherwise the modulo logic won't work
System.out.print("\n");
}
}

Related

Need to get the Least Frequent element of a 2D array

int curr = 0;
int cnt;
String element = values[0][0];
int numberRepeats = 0;//cnt-counter,what's the element ,how many times it's repeated
for (int i = 0; i < values.length; i++) {
for (int j = 0; j < values[i].length; j++) {//those two for's are for the current element
cnt = 0;//counter is nullified
for (int j2 = i; j2 < values.length; j2++) {
for (int k = 0; k < values[j2].length; k++) {//and those two are the compared element
if (values[i][j] == values[j2][k]) {//if the current element is the same as the compared element,increase counter
cnt++;
}
}
if (cnt >numberRepeats) {//after the compared element is done comparing and the number of repeats of the current element is more then the lastly checked element
element = values[i][j];//we get the element ,and how many times it's repeated
numberRepeats = cnt;
}
}
}
}
System.out.println();
System.out.println("The most popular item is: "+element+". Number sold:"+numberRepeats);`
This is what I currently get:
houseShampoo meatPork dairyCream wheatBread wheatCrackers
houseShampoo houseShampoo houseDetergent meatPork dairyYogurt
meatLamb dairyMilk dairyCream meatPork houseShampoo
wheatCookies meatLamb dairyYogurt wheatCereal wheatBread
meatLamb dairyMilk wheatCookies wheatCrackers wheatPasta
The most popular item is: houseShampoo. Number sold:4
This is what I want to get:
The least popular item is: wheatPasta. Number sold:1
But I don't know how to change the condition in the if statement to give out the least frequent element instead of most popular
Since you want to find the minimum number of repeats, start of your initial value with the maximum value:
int numberRepeats = Integer.MAX_VALUE;
and when an item is found with a smaller number of repeats, replace it:
if (cnt < numberRepeats) {
element = values[i][j];
numberRepeats = cnt;
}
This should do the trick.
Hi I choosed different approach using Map I hope it helps :)
int dimension = 3;
int sold = 0;
String product = "";
Map<String,Integer> productsSoldAndCount = new HashMap<>();
String[][] elements = new String[3][3];
elements[0] = new String[]{"houseShampoo","meatpork","dairyCream"};
elements[1] = new String[]{"houseShampoo","bread","cookies"};
elements[2] = new String[]{"meatpork","meatpork","meatpork"};
for(var tmpArray : elements){
for(var tmpString : tmpArray){
if(!productsSoldAndCount.containsKey(tmpString)){
productsSoldAndCount.put(tmpString,1);
}else{
productsSoldAndCount.put(tmpString,productsSoldAndCount.get(tmpString) + 1);
}
}
}
for(var resultPair : productsSoldAndCount.entrySet()){
if(resultPair.getValue() > sold){
sold = resultPair.getValue();
product = resultPair.getKey();
}
}
System.out.println("The most popular item is: " + product + " Sold: " + sold);

Java Removing Redundant Items in Array

For this particular problem I am attempting to remove redundant elements in an sorted array and replace them all with 0s at the end of the array. For example, if I had an array consisting of the int elements
1,3,3,4,4,5,6,6,7
My output array should be
1,3,4,5,6,7,0,0,0
My first attempt at the problem was to create a swapper in order to push all the 0s to the end of the list after removing the elements, but it won't seem to push the zeros to the end of the list. Here is my code.
public void implode(int[] ary)
{
int swapper = -1;
int[] newARY = new int[ary.length];
int current = -1;
for (int i = 0; i < ary.length; i++)
{
if (current != ary[i])
{
newARY[i] = ary[i];
current = ary[i];
}
}
for (int i = 0; i < ary.length; i++)
{
if (ary[i] == 0)
{
if (ary[i + 1] != 0)
{
swapper = ary[i + 1];
ary[i] = swapper;
ary[i + 1] = 0;
}
}
}
ary = newARY;
for (int i = 0; i < newARY.length; i++)
{
System.out.print(newARY[i] + " ");
}
}
The array im testing it with is,
int[] aryIn2 = {1, 1, 2, 3, 4, 4, 5, 6};
However, when outputting the imploded array, I receive this one.
1 0 2 3 4 0 5 6
Is there something I am missing?
Thanks in advance.
not an answer to your problem, but using (if possible) java streams can shorten your way:
int[] arr = {1,3,3,4,4,5,6,6,7};
// distinct
List<Integer> list = Arrays.stream(arr).distinct().boxed().collect(Collectors.toList());
// pad with zero's
while(list.size() < arr.length) {
list.add(0);
}
// display
System.out.println(list.stream().map(String::valueOf).collect(Collectors.joining(",")));
will output
1,3,4,5,6,7,0,0,0
Two issue with you code that I observed.
1) Your swapper logic is performing swapping on a different array than the one in which you had done modification earlier
2) You need to have this logic in a bubble-sort way, i.e. loop inside a loop
Below is a working modified sample code of your method. I have modified only the second for-loop logic
public void implode(int[] ary) {
int swapper = -1;
int[] newARY = new int[ary.length];
int current = -1;
for (int i = 0; i < ary.length; i++) {
if (current != ary[i]) {
newARY[i] = ary[i];
current = ary[i];
}
}
for (int i = 0; i < newARY.length - 1; i++) {
if (newARY[i] == 0 && newARY[i + 1] != 0) {
for (int j = i; (j + 1) < newARY.length; j++) {
swapper = newARY[j + 1];
newARY[j] = swapper;
newARY[j + 1] = 0;
}
}
}
for (int i = 0; i < newARY.length; i++) {
System.out.print(newARY[i] + " ");
}
}
In this first loop:
for (int i = 0; i < ary.length; i++) {
if (current != ary[i]) {
newARY[i] = ary[i];
current = ary[i];
}
}
You fill newARY with elements in ary with duplicated value turns to 0:
newARY: 1 0 2 3 4 0 5 6
However, in the second loop:
for (int i = 0; i < ary.length; i++)
{
if (ary[i] == 0)
{
if (ary[i + 1] != 0)
{
swapper = ary[i + 1];
ary[i] = swapper;
ary[i + 1] = 0;
}
}
}
You're modifying your original ary array. So the newARY is not updated.
However, your attempt to push 0 to the end of array also fail if there are more than two 0s consecutive. And it is also vulnerable to ArrayOutOfBoundIndexException since you try to read ary[i+1] without restriction on i
One simple and straight forward way to push 0s to the end of the array is to create new array with non-0s elements and fill 0s later:
int[] result = new int[ary.lenght];
int resultIndex = 0;
for (int i = 0; i < newARY.length; i++) {
if (newARY[i] != 0) {
result[resultIndex++] = newAry[i];
}
}
for (int i = resultIndex; i < newARY.length; i++) {
result[i] = 0;
}
// Print result array
Hint: Using above strategy, you can simplify your code. No need to create immediate array newARY. Just loop over the original array, push unique elements to the result array, then fill any slot left with 0s.

Fill array within nested loops

I am trying to fill an array in two nested loops however for each second pPiece[] i want to give it a k attribute of 0 or 1 every second pPiece[] respectively
For example -
pPieces[0] = new Piece(0,pcName,1);
pPieces[1] = new Piece(1,pcName,1);
pPieces[2] = new Piece(0,pcName,1);
pPieces[3] = new Piece(1,pcName,1);
etc....
What i have
private Piece pPieces[] = new Piece[8];
for(int j=0; j<pCount; j++) //pCount = 4
{
for(int k=0; k<pcCount; k++) //pcCount = 2
{
String pcName = "Piece " + (allocation());
pPieces[j+k] = new Piece(k,pcName,1);
}
}
Doing it this way results in pPieces[] index being over written 4 times, i think. Is it possible to properly fill this array which should have 8 objects stored in it with every second 'k' equaling 0 or 1 respectively?
The issue in your current solution is that j+k will get the same value multiple times during the two loops:
for(int j=0; j<pCount; j++) //pCount = 4
{
for(int k=0; k<pcCount; k++) //pcCount = 2
{
String pcName = "Piece " + (allocation());
pPieces[j+k] = new Piece(k,pcName,1);
}
}
For example, when j = 0 and k = 1, you will have j + k = 1. But you will also have that when j = 1 and k = 0.
The problem comes from the fact that you're incrementing the variable j by steps of 1 when you should increment it by steps of pcCount; and the related issue is that j should go to pCount*pcCount and not pCount only.
for(int j=0; j<pCount*pcCount; j+=pcCount) //<--- j+=pcCount here, not j++
{
for(int k=0; k<pcCount; k++)
{
String pcName = "Piece " + allocation();
pPieces[j+k] = new Piece(k,pcName,1);
}
}
As a side-note, consider using more descriptive variable names instead of pCount and pcCount.
Refactor your code as follows, The issues is pPieces[j+k] = new Piece(k,pcName,1);
Your j+k => 0,1,1,2,2,3,3,4
Corrected Code
private Piece pPieces[] = new Piece[8];
int count=0;
for(int j=0; j<pCount; j++) //pCount = 4
{
for(int k=0; k<pcCount; k++) //pcCount = 2
{
String pcName = "Piece " + (allocation());
pPieces[count++] = new Piece(k,pcName,1);
}
}
Indeed, j+k will be overlapping. But 2j+k does not.
You need to replace by :
private Piece pPieces[] = new Piece[8];
for(int j=0; j<pCount; j++) //pCount = 4
{
for(int k=0; k<pcCount; k++) //pcCount = 2
{
String pcName = "Piece " + (allocation());
pPieces[2*j+k] = new Piece(k,pcName,1);
}
}
}
The % operator can be of use here:
private Piece pPieces[] = new Piece[8];
for(int j = 0; j < pPieces.length; j++) {
String pcName = "Piece " + (allocation());
pPieces[j] = new Piece(j % 2,pcName,1);
}
The % (modulus) operator returns the remainder of integer division.
e.g.
0 % 2 = 0
1 % 2 = 1
2 % 2 = 0
3 % 2 = 1
Creating an alternation of 1 and 0.

Java Program multiples of a number

"Write a snippet of Java code that adds every multiple of 21, starting with 642331 down to 21 to an unidentified ArrayList named "values" "
I have alot of issues with this, but here is my code. I am not sure what is wrong.
for (int =0 ; i = 642331; i++)
{
values [i] = 21*i ;
}
How about this:
ArrayList<Long> values = new ArrayList<>(); // not sure if required by question
for (long l= 642331 / 21; l > 0; --l) {
values.add(l * 21);
}
Try this implementation.
for (int =642331; i > 0; --i)
{
values [i] = 21*i ;
}
Try as follows:
for (int i =642331 ; i>= 21; i--)
{
values [i] = 21*i ;
}
You forget to declare the i variable:
for (int i = 0; i < 642331; i++)
{
values [i] = 21*i ;
}

Put two arrays to Jtable rows

I have two arrays, which is equal in terms of number of elements. I want to put it in a JTable rows (like in the ascii table example from bellow). I'm using table model and a loop for both arrays, but I archive something else (see print screen).
Note: I want to maintain the correspondence between elements of both arrays, like in the ascii table example.
Integer[] intArray = new Integer[stringArray.length];
for (int i = 0; i < stringArray.length; i++) {
intArray[i] = Integer.parseInt(stringArray[i]);
}
System.out.println(Arrays.toString(intArray)); //output [285, 715, 1437, 1749]
Integer[] intArray1 = new Integer[stringArray1.length];
for (int i = 0; i < stringArray1.length; i++) {
intArray1[i] = Integer.parseInt(stringArray1[i]);
}
System.out.println(Arrays.toString(intArray1)); //output [0, 0, 1087, 0]
DefaultTableModel modelPeaks = new DefaultTableModel();
JTable table = new JTable(modelPeaks);
modelPeaks.addColumn("1st Column");
modelPeaks.addColumn("2nd Column");
for (int i = 0; i < intArray.length; i++) {
for (int j = 0; j < intArray1.length; j++) {
modelPeaks.addRow(new Object[]{intArray[i], intArray1[j]});
}
}
The output is:
But I want to archive this:
+--------------------+--------------------+
+ 1st Column + 2nd Column +
+--------------------+--------------------+
+ 285 + 0 +
+ 715 + 0 +
+ 1437 + 1087 +
+ 1749 + 0 +
+--------------------+--------------------+
I think that is from the loop, but I can't figure out how to fix it. Someone can help me? And thanks in advance for your time.
The last loop should read
for (int i = 0; i < intArray.length; i++) {
modelPeaks.addRow(new Object[]{intArray[i], intArray1[i]});
}
Always make sure that both array have the same length.
If the arrays are both the same length you can use the same iterator for both:
for (int i = 0; i < intArray.length; i++) {
modelPeaks.addRow(new Object[]{intArray[i], intArray1[i]});
}

Categories