Class not showing up in run configurations? - java

I have a simple class that takes out items that were already in another array from the first array, by putting it into a list. Whenever I run this program though, another program that I created starts to run. I have tried to change the run configuration as well, but it doesn't show up on the list..? Here is the code:
package collections;
import java.util.*;
public class Arraystring {
public static void main(String [ ] args, int Collection, int String){
String[] things = { "eggs", "lasers", "hats", "pie" };
List<String> list1 = new ArrayList<String>();
for (String x : things){ //enhanced for loop
list1.add(x);
}
System.out.println(list1);
String[] thingstwo = { "lasers", "hats" };
List<String> list2 = new ArrayList<String>();
for(int i = 0;i<list2.size(); /* size for list, length for array */ i++) //regular for loop
{
list2.add(thingstwo[i]);
}
//print list one
for(int i = 0;i<list1.size(); /* size for list, length for array */ i++) //regular for loop
{
System.out.println(list1.get(i)); //Use .get for lists instead of []
}
editlist(list1, list2);
System.out.println();
//print list one
for(int i = 0;i<list1.size(); /* size for list, length for array */ i++) //regular for loop
{
System.out.println(list1.get(i)); //Use .get for lists instead of []
}
}
public static void editlist(Collection<String> l1, Collection<String> l2){
Iterator<String> it = l1.iterator(); //Goes through each list item by item
while (it.hasNext()){
if(l2.contains(it.next())){
it.remove();
}
System.out.println(it);
}
}
}
Thanks for helping, I really appreciate it.

You can execute Run file command from menu/shortcut or change main project in the IDE.
Specify yours IDE and proper shortcuts/commands would be given to you.
Upd.
You have error in yours logic:
String[] thingstwo = {"lasers", "hats"};
List<String> list2 = new ArrayList<>();
for (int i = 0; i < list2.size(); i++)
list2.add(thingstwo[i]);
Look at it line-by-line:
String[] thingstwo = {"lasers", "hats"};
Defining array, ok.
List<String> list2 = new ArrayList<>();
Defining list of strings, ok.
for (int i = 0; i < list2.size(); i++)
Looping throug items of list list2, not ok. list2 at this point is empty. Assuming you wanted to loop through thingstwo and add it's items to list2, so fixing the code:
for (int i = 0; i < thingstwo.length; i++)
And...
list2.add(thingstwo[i]);
Adding i-th item of thingstwo to list2, ok.
Running:
[eggs, lasers, hats, pie]
eggs
lasers
hats
pie
eggs
pie

Related

How to remove certain elements from my array in my Java code

I am working on the following coding prompt for my class:
Your task is to write a method with the following signature:
public static String[] removeFromArray(String[] arr, String toRemove)
The method should return a string array that has the same contents as arr, except without any
occurrences of the toRemove string. For example, if your method is called by the code below
String[] test = {“this”, “is”, “the”, “example”, “of”, “the”, “call”};
String[] result = removeFromArray(test, “the”);
System.out.println(Arrays.toString(result));
it should generate the following output:
[this, is, example, of, call]
Note: Your method will be passed values for arr and toRemove by the testing program – you should not
read these values in from the user inside your method. Also, you must write this method with the
signature requested above in order to receive credit. You do not need to write the code that calls the
method – only the method itself.
Hint: Because you must specify the length of an array when you create it, you will likely need to make
two loops through the input array: one to count the number of occurrences of the toRemove string so
that you can create the new array with the proper size and a second to copy all of the other strings to the new array.
I have everything working in my code but the last part where I have to print out the new array does not work, I know I have make it smaller so it will print out properly, but I can't get that part to work. I know I have to get rid of the null, but I don't know how. Also my code has to work for any array not just the test case I have. Some help or advice would really be nice. Thank you very much!!! :)
Here is my code:
public static void main(String[] args) {
String[] test = {"this", "is", "the", "example", "of", "the", "call"};
String[] remove = removeFromArray(test, "the");
System.out.println(Arrays.toString(remove));
}
public static String[] removeFromArray(String[] arr, String toRemove) {
int count = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i].equals(toRemove)) {
count++;
}
}
String[] result = new String[arr.length - count];
//for (int i = 0; i < arr.length; i++) {
// if(!arr[i].equals(toRemove)){
// result[].equals(arr[i]);
//}
//}
return result;
}
you approach looks ok, it looks like the commented code yor are trying to assign the new array with the wrong emthod
you should use result[i] = arr[i] ; instead of result[].equals(arr[i]);
do at the end:
String[] result = new String[arr.length - count];
int k = 0;
for (int i = 0; i < arr.length; i++) {
if(!toRemove.equals(arr[i])){
result[k] = arr[i];
k++;
}
}
return result;
Your last part should be assigning the value to the array one by one.
int j = 0;
for (int i = 0; i < arr.length; i++) {
if(!toRemove.equals(arr[i])){
result[j++] = arr[i];
}
}
It's asking you to return a new String array which excludes the given word. Loop through the array and add word which does not equal to the given word.
public static String[] removeFromArray(String[] arr, String toRemove){
ArrayList<String> words = new ArrayList<>();
for(String s : arr)
if(!s.equals(toRemove))
words.add(s);
return words.toArray(new String[0]);
}
Since array size cannot be changed after being created, use an ArrayList to store the words, then return as an array.
I know you're new to programming itself, so the solutions given are perfectly fine.
However, using Java, you'd usually use the libraries; in this case, the Collections library. If you're using Java 8, this is how you would do it:
public static String[] removeFromArray(String[] arr, String toRemove) {
// create a List and fill it with your items
ArrayList<String> list = new ArrayList();
Collections.addAll(list, arr);
// remove the items that are equal to the one to be removed
list.removeIf(s -> toRemove.equals(s));
// transfer back to array
return list.toArray(new String[list.size()]);
}
Then, there are Java 8 Streams, which would make this
public static String[] removeFromArray(String[] arr, String toRemove) {
return Arrays.stream(arr) // create stream from array
.filter(s -> !toRemove.equals(s)) // filter out items
.toArray(String[]::new); // create array from stream
}

ArrayList iteration specific list

I have an arraylist in which
ArrayList<ArrayList<String>> listofItems=new ArrayList<ArrayList<String>>();
in the list of items I have items like this
[[1][2][3][4][5][6][7][8][9][10][11][12][13][14][15][16][17][18][19][20][21][22]]
how to iterate and store in another array these values split into two like
[1,2,3,4,5,6,7,8,9,10,11] and [11,12,13,14,15,16,17,18,19,20,21,22]. I have used advanced for loop
for(ArrayList<String> list:listofItems)
{
for (String s:list)
{
//I dont know how to add logic here.
}
}
I am assuming here that both lists will also hold string type data.If you want Integer type,you can parse while adding.
ArrayList<ArrayList<String>> listofItems = new ArrayList<ArrayList<String>>();
ArrayList<String> list1 = new ArrayList<String>();
ArrayList<String> list2 = new ArrayList<String>();
for (ArrayList<String> list : listofItems) {
for (String s : list) {
// there shud be some condition how much elements you want in a
// list or some condition
// to decide in which list we need to add item
if (list.size() < 12)
list1.add(s);
else
list2.add(s);
}
}
Use something like that:
if (yourlist.size()>0) {
List<String> first = yourList.subList(0, (int)Math.ceil(yourlist.size()/2.0));
List<String> second = yourList.subList((int)Math.ceil(yourlist.size()/2.0), yourlist.size());
}
Would be separate your list in 2 lists divided in the half.
You can use this code to store your list into another list.
List<String> stringList = new ArrayList<String>();
for(ArrayList<String> list:listofItems) {
for (String s:list) {
stringList.add(s);
}
}
In this case, when you add info to listOfItems, you should try example like this
for(int i = 0; i< 2; i++){
for(int j = 0; j < n; j++){
/// add util break n
lists.get(i).add(strTemp);
}
}
You can change i < 2 into any number you want. I only make this sample for you.

Extract String arrays from List

Developing a Java Android App but this is a straight up Java question i think.
I have List declared as follows;
List list= new ArrayList<String[]>();
I want to extract each String [] in a loop;
for(int i=0; i < list.size(); i++) {
//get each String[]
String[] teamDetails = (String[])list.get(i);
}
This errors, I am guessing it just doesn't like me casting the String[] like this.
Can anyone suggest a way to extract the String[] from my List?
Use a List<String[]> and you can use the more up-to-date looping construct:
List<String[]> list = new ArrayList<String[]>();
//I want to extract each String[] in a loop;
for ( String[] teamDetails : list) {
}
// To extract a specific one.
String[] third = list.get(2);
Try declaring the list this way
List<String[]> list = new ArrayList<>();
for(int i = 0; i < list.size(); i++) {
//get each String[]
String[] teamDetails = list.get(i);
}
Moreover the call of your size function was wrong you need to add the brackets
/*ArrayList to Array Conversion */
String array[] = new String[arrlist.size()];
for(int j =0;j<arrlist.size();j++){
array[j] = arrlist.get(j);
}
//OR
/*ArrayList to Array Conversion */
String frnames[]=friendsnames.toArray(new String[friendsnames.size()]);
In for loop change list.size to list.size()
And it works fine Check this https://ideone.com/jyVd0x
First of all you need to change declaration from
List list= new ArrayList<String[]>(); to
List<String[]> list = new ArrayList();
After that you can do something like
String[] temp = new String[list.size];
for(int i=0;i<list.size();i++)`
{
temp[i] = list.get(i);
}

How to remove repeated items of a list and copy the list into another list

I have a list of items that need to remove their repeated ones and then copy the list into another list. The problem is that I cant copy the list into the other list.
Code
.....
private List mylist = new ArrayList();
.....
LinkedHashSet hs = new LinkedHashSet();
hs.addAll(mylist);
mylist.clear();
mylist.addAll(hs);
MyClass.getItems().clear();
MyClass.setItems(mylist);
MyClass.java
.....
private List Items = new ArrayList();
public void setItems(List myItems) {
for (int i = 0; i < myItems.size(); i++) { <<This loop shows the items
System.out.println(myItems.get(i));
}
this.Items.clear();
this.Items.addAll(myItems);
for (int i = 0; i < Items.size(); i++) { << this loop does not show anything
System.out.println(Items.get(i));
}
}
Desired result
mylist >> a,b,c,a,d,c
change to a,b,c,d
then copy to items
items >> a,b,c,d
you can use a LinkedHashSet. This will preserve the insertion order and insure no duplicates.
for problem number two are you adding anyting to the list. the code you have works
List<Integer> mylist = new ArrayList<Integer>();
mylist.add(3);
mylist.add(3);
HashSet hs = new HashSet();
hs.addAll(mylist);
mylist.clear();
mylist.addAll(hs);
System.out.println(mylist.size()); //prints 1
System.out.println(hs.size());// prints 1
RESPONSE to edited question:
They both seem to print out the list fine
public class Tmp {
private List<Integer> Items = new ArrayList<Integer>();
public void setItems(List<Integer> myItems) {
for (int i = 0; i < myItems.size(); i++) { //<<This loop shows the items
System.out.println(myItems.get(i));
}
this.Items.clear();
this.Items.addAll(myItems);
System.out.println();
for (int i = 0; i < Items.size(); i++) { //<< this loop also shows the item
System.out.println(Items.get(i));
}
}
public static void main(String[] args) {
Tmp t = new Tmp();
List<Integer> myList = new ArrayList<Integer>();
myList.add(3);
myList.add(4);
t.setItems(myList);
}
}
Are both these pieces of code in the same class and both list variables pointing to the same arrayList instance. if so calling clear on one clears out both lists (since both variables are pointing to the same list)

printing elements of array in arraylist in java

I do have this code, and I would like to print out all the array's values of Arraylist.
thanks for your help in advanced.
here is my code:
for (int i = 0; i <count; i++) {
System.out.println("list #" + i);
for (int j = 0; j < list[i].size(); j++) {
list[i].get(j);
System.out.println("elements of array in arraylist "+list[i].get(j));
}
}
For printing elements of an array stored in arraylist,you will have to to do the following:
for each element of arraylist
get array from arraylist
for each array element in array
print array element.
You seemed to be iterating array of List type instead.
Edit your code with further detail on your data structure
for (Object[] array : list)
for (Object o : array)
System.out.println("item: " + o);
See if this can work for you. I think it's simpler:
int numLists = 10; // Or whatever number you need it to be.
ArrayList [] arrayOfLists = new ArrayList[numLists];
// you realize, of course, that you have to create and add those lists to the array.
for (ArrayList list : arrayOfLists) {
System.out.println(list);
}
I'd wonder why you don't prefer a List of Lists:
List<List<String>> listOfLists = new ArrayList<List<String>>();
// add some lists of Strings
for (List<String> list : listOfLists) {
System.out.println(list);
}
Below code works fine for me
public class Solution
{
public static void main(String[] args)
{
int T,N,i,j,k=0,Element_to_be_added_to_the_array;
Scanner sn=new Scanner(System.in);
T=sn.nextInt();
ArrayList<Integer>[] arr=new ArrayList[T];
for(i=0;i<T;i++)
{
arr[k]=new ArrayList<Integer>();
N=sn.nextInt();
for(j=0;j<N;j++)
{
Element_to_be_added_to_the_array=sn.nextInt();
arr[k].add(Element_to_be_added_to_the_array);
}
k++;
}
//Printing elements of all the arrays contained within an arraylist
for(i=0;i<T;i++)
{
System.out.println("array["+i+"]");
for(j=0;j<arr[i].size();j++)
{
System.out.println(arr[i].get(j));
}
}
}
}

Categories