Python中文网

一个关于 编程问题的解答网站.

有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java从字符串数组的ArrayList中删除字符串的可能逻辑?

有人能透露从字符串数组的ArrayList中删除字符串的任何可能逻辑吗

ArrayList <String[]> test = new ArrayList<>();

test.remove(i)[k]; won't work. Where 'i' and 'k' are possible loops.

实际上,我正在尝试制作集成求解器。我已经通过split方法拆分了一个完整的字符串输入,然后将整个字符串转换为arraylist等等。代码可以被视为: 请考虑^ {CD2>}注释

import java.util.ArrayList;
import java.util.Arrays;


public class IntegrationClass {

public static final String calculate(String input)
{
    String rawPositive[];
    String rawNegInitial[];
    String[] rawNeg=null;
    String SplitPosArray[] = null;
    String SplitNegArray[]=null;

    ArrayList<String[]> rawNeg1= new ArrayList<>();
    ArrayList <String> DataNeg = new ArrayList<>();
    ArrayList <String> DataPos = new ArrayList<>();
    ArrayList <String> SplitPos =  new ArrayList<>();
    ArrayList <String> SplitNeg =  new ArrayList<>();
    ArrayList <ArrayList> PosPower= new ArrayList<>();
    ArrayList <ArrayList> NegPower= new ArrayList<>();


    ArrayList <String> tempX = new ArrayList<>();       

    if (input.contains("+")==true||input.contains("-")==true)
    {

        rawPositive= input.split("\\+");
        //casting raw positive into array list
        DataPos = new ArrayList<>(Arrays.asList(rawPositive));

        //putting positive final data into raw initial data

            rawNegInitial= DataPos.toArray(new String[DataPos.size()]);


        // deleting negative value from positive final
        for(int i =0;i<DataPos.size();i++)
        {
        if(DataPos.get(i).contains("-"))
        {
            String temp[];
            temp=DataPos.get(i).split("\\-");
            DataPos.set(i, temp[0]);
        }
        }


            //putting initial data in raw of negative from positive data

            for(int i =0;i<rawNegInitial.length;i++)
            {
                //System.out.println(rawNegInitial[i]);
                if(rawNegInitial[i].contains("-")==true)
                {
                    rawNeg=rawNegInitial[i].split("\\-");
                    rawNeg1.add(rawNeg);
                }
            }

            //casting negative raw into final negative data
            try{
            //  DataNeg = new ArrayList<>(Arrays.asList(rawNeg1));

            //deleting positive data from negative final
            for(int j=0;j<rawNeg1.size();j++)
            {
                for(int k=0;k<2;k++)
                {
            for(int i =0;i<DataPos.size();i++)
            {

                    String a=rawNeg1.get(j)[k];
                    System.out.println(DataPos.get(i) +" "+ a);
                    if(DataPos.get(i).contains(a))
                    {

                        //x^1+x^2-x^3+x^4-x^5
                        System.out.println("here");
                        try{
                        rawNeg1.get(j)[k-1]=rawNeg1.get(j)[k];
                        }
                        catch(ArrayIndexOutOfBoundsException ex)
                        {

                            rawNeg1.get(j)[k]=rawNeg1.get(j)[k+1];
                        }
                        //rawNeg1.get(j)[k]= null;
                    }
                    else
                    {
                        tempX.add(a);
                    }

                    System.out.println(rawNeg1.get(j)[k]+ " yoy ");
            }
                }
            }
            //  DataNeg = new ArrayList<>(tempX);

            //adding minus sign with negative array.
            for(int i=0;i<DataNeg.size();i++)
            {
                String x=DataNeg.get(i);
                x="-"+x;
                DataNeg.set(i, x);
            }
            }
            catch(NullPointerException np)
            {

            }
            //splitting ^ from PosArrayList
            for(int i =0;i<DataPos.size();i++)
            {
                if(DataPos.get(i).contains("^"))
                {
                    SplitPosArray=DataPos.get(i).split("\\^");
                    SplitPos = new ArrayList<>(Arrays.asList(SplitPosArray));
                    PosPower.add(SplitPos);
                }
            }

            //splitting ^ from NegArrayList
            for(int i =0;i<DataNeg.size();i++)
            {
                if(DataNeg.get(i).contains("^"))
                {
                    SplitNegArray=DataNeg.get(i).split("\\^");
                    SplitNeg = new ArrayList<>(Arrays.asList(SplitNegArray));
                    NegPower.add(SplitNeg);
                }
            }






            // checking original positive data
            for(int i=0;i<DataPos.size();i++)
            {
                System.out.println(DataPos.get(i));
            }

            //checking original negative data

            for(int i =0;i<rawNeg1.size();i++)
            {
                System.out.println(rawNeg1.get(i)[i]);
            }
            //checking sillppted postive
            for(int i=0;i<PosPower.size();i++)
            {
                for(int j=0;j<2;j++)
                {
                System.out.println(PosPower.get(i).get(j));
            }
                }
            //checking splitted negaitve
            for(int i=0;i<NegPower.size();i++)
                {
                    for(int j=0;j<2;j++)
                    {
                    System.out.println(NegPower.get(i).get(j));
                }
                    }
    }

    return "return";
 }
}

共 (1) 个答案

  1. # 1 楼答案

    不能“删除”数组中的元素。您可以将其指定为某个哨兵值,例如null

    List<String[]> test = ...
    test.get(i)[k] = null;
    

    相反,您可以将其实现为列表列表:

    List<List<String>> test = ...
    test.get(i).remove(k);